Cod sursa(job #612824)

Utilizator cmiNCosmin Poieana cmiN Data 10 septembrie 2011 14:14:26
Problema Problema rucsacului Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 0.92 kb
#include <stdio.h>


typedef unsigned int ui_t;
struct object {
    ui_t weight, value;
} objects[5001];
ui_t objnr, sacwg, cost[5001][10001];

ui_t maxcost(ui_t a, ui_t b)
{
    return a > b ? a : b;
}

void process()
{
    for (ui_t i = 1; i <= objnr; i++) {
        for (ui_t j = 1; j <= sacwg; j++) {
            if (objects[i].weight > j) {
                cost[i][j] = cost[i - 1][j];
            } else {
                cost[i][j] = maxcost(cost[i - 1][j], cost[i - 1][j - objects[i].weight] + objects[i].value);
            }
        }
    }
}

int main()
{
    freopen("rucsac.in", "r", stdin);
    freopen("rucsac.out", "w", stdout);
    scanf("%u %u", &objnr, &sacwg);
    for (ui_t i = 1; i <= objnr; i++) {
        scanf("%u %u", &(objects[i].weight), &(objects[i].value));
    }
    process();
    printf("%u", cost[objnr][sacwg]);
    fclose(stdin);
    fclose(stdout);
    return 0;
}