Pagini recente » Cod sursa (job #610341) | Cod sursa (job #3221983) | Cod sursa (job #1074937) | Cod sursa (job #2232770) | Cod sursa (job #2644132)
#include <stdio.h>
int main(int argc, char ** args) {
freopen("rucsac.in" , "r", stdin);
freopen("rucsac.out", "w", stdout);
int i, n, gmax, currentP, currentG, currentGMax, temporaryGMax;
scanf("%d%d", &n, &gmax);
int profits[gmax+1];
for(i=0; i<=gmax; ++i)
profits[i] = 0;
profits[0] = 1;
currentGMax = 0;
for(i=0; i<n; ++i) {
scanf("%d%d", ¤tG, ¤tP);
temporaryGMax = currentGMax;
for(int j = currentGMax; j>=0; --j) {
if (profits[j] != 0)
if (j + currentG <= gmax && profits[j] + currentP > profits[j + currentG]) {
profits[j + currentG] = profits[j] + currentP;
if (j + currentG > currentGMax)
temporaryGMax = j + currentG;
}
}
currentGMax = temporaryGMax;
}
temporaryGMax = 0;
for(i=0; i<= gmax; ++i)
if (profits[i] > temporaryGMax)
temporaryGMax = profits[i];
printf("%d\n", temporaryGMax - 1);
return 0;
}