Cod sursa(job #1496829)

Utilizator trust2014Alex Murariu trust2014 Data 5 octombrie 2015 17:41:16
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.5 kb
#include <iostream>
#include <fstream>


using namespace std;

ifstream f ("in.in");
ofstream g ("out.out");

const int NMax = 5005;
int D[NMax][NMax];

int main()
{
int n, Gmax, x, value, i, j;
f >> n >> Gmax;
for(i = 1; i <= n; i++)
    {
    f >> x >> value;
     for(j = 1; j <= Gmax; j++)
        if(j >= x)
     {
         D[i][j] =  max(D[i - 1][j], D[i - 1][j - x] + value);
     }
     else
     {
         D[i][j] = D[i - 1][j];
     }
}
    g << D[n][Gmax];
return 0;
}