Pagini recente » Cod sursa (job #2097428) | Cod sursa (job #2873132) | Cod sursa (job #434703) | Cod sursa (job #3205765) | Cod sursa (job #2486673)
#include <fstream>
using namespace std;
ifstream cin("rucsac.in");
ofstream cout("rucsac.out");
int n, g;
int w[5000], p[5000];
int *now = new int[10002], *old = new int[10002];
void upd(int x, int y, int val){
now[y] = max(now[y], val);
}
int main() {
cin>>n>>g;
for(int x = 0; x<n; x++)
cin>>w[x]>>p[x];
for(int x = 0; x<=10000; x++) {
//b[x] = false;
now[x] = 0;
old[x] = 0;
//b[x] = -1;
}
//b[0] = 0;
for(int x = 0; x<n; x++){
for(int y = 0;y<=10000;y++){
//if((b[y] == x) || (b[y] == (x + 1))){
upd(x, y, old[y]);
if((y + w[x]) <= g)
upd(x, y + w[x], old[y] + p[x]);
//}
}
swap(now, old);
}
int maxv = -1;
for(int x = 0;x<=g;x++)
maxv = max(maxv, old[x]);
cout<<maxv;
return 0;
}