Pagini recente » Cod sursa (job #2198286) | Cod sursa (job #2164835) | Cod sursa (job #1678028) | Cod sursa (job #500655) | Cod sursa (job #2636691)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int w[5010], p[5010], best[5010][10010];
int main()
{
int n, g;
in >> n >> g;
for(int i = 1; i <= n; i++)
{
in >> w[i] >> p[i];
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= g; j++)
{
if(j >= w[i])
best[i][j] = max(best[i-1][j], best[i-1][j - w[i]] + p[i]);
else
best[i][j] = best[i-1][j];
}
}
out << best[n][g];
return 0;
}