Pagini recente » Cod sursa (job #2535873) | Cod sursa (job #1418735) | Cod sursa (job #235449) | Cod sursa (job #2330651) | Cod sursa (job #1469204)
#include <iostream>
#include <fstream>
#include <vector>
#define MAX 10005
#define LL long long
using namespace std;
int pos[MAX];
int prof[MAX];
int main() {
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, g, x, maxv = 0;
vector <int> w, p;
fin >> n >> g;
for (int i = 0; i < n; ++i) {
fin >> x;
w.push_back(x);
fin >> x;
p.push_back(x);
}
pos[0] = 1;
prof[0] = 0;
for(int i = 0; i < n; ++i) {
for(int j = g - w[i]; j >= 0; --j) {
if(pos[j]) {
pos[j + w[j]] = 1;
prof[j + w[j]] = max(prof[j+w[i]], prof[j] + p[i]);
maxv = max(prof[j + w[j]], maxv);
}
}
}
fout << maxv;
return 0;
}