Pagini recente » Cod sursa (job #618338) | Cod sursa (job #871810) | Cod sursa (job #2234423) | Cod sursa (job #2559498) | Cod sursa (job #1450755)
#include <stdio.h>
#define NMAX 5000
#define GMAX 10000
struct object {
unsigned weight, profit;
} Object[NMAX + 1];
unsigned long DP[NMAX + 1][GMAX + 1];
int main(void)
{
unsigned i, n, G, g;
freopen("rucsac.in", "r", stdin);
freopen("rucsac.out", "w", stdout);
scanf("%u %u", &n, &G);
for (i = 1; i <= n; i++)
scanf("%u %u", &Object[i].weight, &Object[i].profit);
for (i = 1; i <= n; i++)
for (g = 0; g <= G; g++) {
DP[i][g] = DP[i - 1][g];
if (Object[i].weight <= g && DP[i - 1][g - Object[i].weight] + Object[i].profit > DP[i][g])
DP[i][g] = DP[i - 1][g - Object[i].weight] + Object[i].profit;
}
printf("%lu\n", DP[n][G]);
return 0;
}