Pagini recente » Cod sursa (job #18232) | Cod sursa (job #2420718) | Cod sursa (job #2893812) | Cod sursa (job #1000848) | Cod sursa (job #1707891)
#include <fstream>
#include <algorithm>
using namespace std;
int Nr, G, Pmax;
bool u;//schimb
int W[5100], P[5100];
int help[2][11000];
int main()
{ ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
fin >> Nr >> G;
for(int i = 1; i <= Nr; ++i)
fin >> W[i] >> P[i];
for(int i = 1; i <= Nr; ++i){
for(int cw = 0; cw <= G; ++cw)
{
help[u][cw] = help[!u][cw];
if(W[i] <= cw)
help[u][cw] = max(help[u][cw], help[!u][cw - W[i]] + P[i]);
}
u = !u;
} u = !u;
Pmax = help[u][G];
fout << Pmax;
return 0;