Pagini recente » Cod sursa (job #1972984) | Cod sursa (job #1009644) | Cod sursa (job #2670180) | Cod sursa (job #2902435) | Cod sursa (job #2445202)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int n, g;
struct ob{
int w, p;
};
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
ob obs[5041];
int tab[5041][10041];
int main()
{
fin >> n >> g;
for(int i = 1; i <= n; i++){
fin >> obs[i].w >> obs[i].p;
}
for(int i = 1; i <= n; i++){
ob cob = obs[i];
for(int w = 1; w <= g; w++){
if(cob.w <= w){
tab[i][w] = max(tab[i-1][w-cob.w]+cob.p, tab[i-1][w]);
}else{
tab[i][w] = tab[i-1][w];
}
}
}
fout << tab[n][g];
}