Pagini recente » Cod sursa (job #995040) | Cod sursa (job #646544) | Cod sursa (job #269350) | Cod sursa (job #2236675) | Cod sursa (job #1895764)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int main() {
int nrOb, G, ans = -1; fin >> nrOb >> G;
vector<int> w, p, DP; w.assign(nrOb, 0); p.assign(nrOb, 0); DP.assign(G + 5, 0);
for(int i = 0; i < nrOb; i++)
fin >> w[i] >> p[i];
for(int i = 0; i < nrOb; i++)
for(int j = G; j >= w[i]; j--)
{ DP[j] = max(DP[j], DP[j - w[i]] + p[i]); ans = max(ans, DP[j]); }
fout << ans << '\n';
return 0;
}