Cod sursa(job #1450755)

Utilizator caen1c a e n caen1 Data 14 iunie 2015 16:58:40
Problema Problema rucsacului Scor 50
Compilator c Status done
Runda Arhiva educationala Marime 0.73 kb
#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;
}