Cod sursa(job #2051382)

Utilizator AgacheGabrielAgache Gabriel AgacheGabriel Data 28 octombrie 2017 21:34:12
Problema Problema rucsacului Scor 35
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <fstream>

using namespace std;

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

struct object
{
    int gre;
    int val;
}item[5000];

int D[5001][10000],N,i,j,G;

int main()
{
    fin>>N>>G;
    for (i=1;i<=N;i++)
        fin>>item[i].gre>>item[i].val;
    for (i=1;i<=N;i++)
        for (j=1;j<=G;j++)
        {
            if (item[i].gre>j) D[i][j] = D[i-1][j];
            if (item[i].gre==j) D[i][j] = max(D[i-1][j], item[i].val);
            if (item[i].gre<j) D[i][j] = max(D[i-1][j],item[i].val+D[i-1][j-item[i].gre]);
        }
    fout<<D[N][G];
    return 0;
}