Pagini recente » Cod sursa (job #573651) | Cod sursa (job #1623912)
#include <iostream>
#include <fstream>
#define dim 5002
#define Nmax 10002
using namespace std;
ifstream f ("rucsac.in");
ofstream g ("rucsac.out");
int n,G,j,x,i,greut[dim],prof[dim],v_first[Nmax],v_second[Nmax];
int main()
{
f >> n >> G;
for(i = 1; i <= n; i++)
f >> greut[i] >> prof[i];
for(i = 1; i <= n; i++)
{
for(j = 1; j <= G; j++)
{
v_second[j] = v_first[j];
x = prof[i] + v_first[j - greut[i]];
if(j >= greut[i])
v_second[j] = max(v_second[j],x);
}
for(int q = 1; q <= G; q++)
v_first[q] = v_second[q];
}
g << v_first[G];
return 0;
}