Pagini recente » Cod sursa (job #1841948) | Cod sursa (job #1143703) | Cod sursa (job #707145) | Cod sursa (job #706932) | Cod sursa (job #2720715)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int n, g, w[5001], p[5001], d[5001][10001], Pmax;
int main()
{
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
fin >> n >> g;
for (int i = 1; i <= n; i++)
fin >> w[i] >> p[i];
for (int i = 1; i <= n; i++)
for (int cw = 0; cw <= g; cw++)
{
if (w[i] <= cw)
d[i][cw] = max(d[i-1][cw], d[i-1][cw-w[i]] + p[i]);
}
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= g; j++)
cout << d[i][j] << " ";
cout << endl;
}
cout << endl
<< d[n][g];
Pmax = d[n][g];
fout << Pmax;
return 0;
}
/*
6 10
3 7
3 4
1 2
1 9
2 4
1 5
*/