Pagini recente » Cod sursa (job #1930873) | Cod sursa (job #2874377) | Cod sursa (job #2376353) | Cod sursa (job #1284662) | Cod sursa (job #1605097)
#include <cstdio>
#include <algorithm>
using namespace std;
int d[10001], p[5001], w[5001];
int main()
{
int i, n, x, y, g, last = 0, profit = -1, j;
freopen("rucsac.in","r",stdin);
freopen("rucsac.out","w",stdout);
scanf("%d%d", &n, &g);
for(i=1; i<=n; ++i){
scanf("%d%d", &x, &y);
p[i] = y;
w[i] = x;
}
for(i=1; i<=n; ++i)
d[i] = -1;
d[0] = 0;
for(i=1; i<=n; ++i){
for(j = last; j >= 0; --j){
if(j + w[i] <= g && d[j] != -1){
d[j + w[i]] = max(d[j + w[i]], d[j] + p[i]);
if(last < j + w[i])
last = j + w[i];
}
}
}
for(i = g; i >= 0; --i)
if(d[i] > profit)
profit = d[i];
printf("%d", profit);
return 0;
}