Cod sursa(job #2662056)

Utilizator DordeDorde Matei Dorde Data 23 octombrie 2020 13:26:31
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <fstream>
#include <algorithm>
using namespace std;
int const N = 10007;
ifstream f ("rucsac.in");
ofstream g ("rucsac.out");
int dp [N];
int main (){
    int n , G , gmax = 0;
    f >> n >> G;
    for(int i = 1 ; i <= n ; ++ i){
        int w , p;
        f >> w >> p;
        for(int j = gmax ; j ; -- j)
            if (dp [j] && j + w <= G)
                dp [j + w] = max (dp [j + w] , dp [j] + p);
        dp [w] = max (dp [w] , p);
        gmax = min (gmax + p , G);
    }
    g << *max_element (dp + 1 , dp + 1 + G);
    return 0;
}