Cod sursa(job #2470546)

Utilizator Ioan_AnghelIoan Anghel Ioan_Anghel Data 9 octombrie 2019 14:36:41
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <fstream>

using namespace std;

ifstream in("rucsac.in");
ofstream out("rucsac.out");

int profit[1000000], w[10000], p[10000];

int main()
{
    int n, g;
    in >> n >> g;

    for(int i = 1; i <= g; i++){
        profit[i] = -1;
    }
    for(int i = 0; i < n; i++){
        in >> w[i] >> p[i];
    }

    for(int i = 0; i < n; i++){
        for(int j = g - w[i]; j >= 0; j--){
            if(profit[j] != -1 && profit[j] + p[i] > profit[j + w[i]]){
                profit[j + w[i]] = profit[j] + p[i];
            }
        }
    }

    int maxi = 0;
    for(int i = 0; i <=g; i++){
        maxi = max(maxi, profit[i]);
    }
    cout << maxi;

    return 0;
}