Pagini recente » Cod sursa (job #2662367) | Cod sursa (job #2978837) | Cod sursa (job #360225) | Cod sursa (job #2937266) | Cod sursa (job #1813662)
#include <iostream>
#include <fstream>
#define NMAX 5005
#define GMAX 10005
using namespace std;
ifstream fin ("rucsac.in");
ofstream fout ("rucsac.out");
int opt[NMAX][GMAX];
int main ()
{
int n, v;
fin >> n >> v;
for (int i = 0; i <= n; ++i) {
for (int j = 1; j <= v; ++j) {
opt[i][j] = -1;
}
}
int maxn = 0;
for (int i = 1; i <= n; ++i) {
int x, y;
fin >> x >> y;
for (int j = x; j <= v; ++j) {
if (opt[i-1][j-x] != -1) {
opt[i][j] = opt[i-1][j-x] + y;
}
opt[i][j] = max(opt[i][j],opt[i-1][j]);
if (opt[i][j] > maxn) {
maxn = opt[i][j];
}
}
}
/*cout << endl;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= v; ++j) {
cout << opt[i][j] << " ";
}
cout << endl;
}*/
fout << maxn;
return 0;
}