Cod sursa(job #701515)

Utilizator PetcuIoanPetcu Ioan Vlad PetcuIoan Data 1 martie 2012 16:18:11
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include<stdio.h>
#include<assert.h>

#include<algorithm>

using namespace std;

const int knmax = 10010;
int sol, no_items, weight, item_weight[knmax / 2], item_profit[knmax / 2], d[knmax];

void read(){
    assert(freopen("rucsac.in","r",stdin)!=NULL);
    scanf("%d%d",&no_items ,&weight);
    for(int i = 1; i <= no_items; ++i)
        scanf("%d%d",&item_weight[i] ,&item_profit[i]);
}

void solve(){
    int i, j, k = 0;
    for(i = 1; i <= no_items; ++i)
        for(j = k; j >= 0; --j)
            if(j + item_weight[i] <= weight){
                if(j + item_weight[i] > k)
                    k = j + item_weight[i];
                d[j + item_weight[i]] = max(d[j + item_weight[i]], d[j] + item_profit[i]);
            }
    for(j = k; j > 0; --j)
        sol = max(sol, d[j]);
}

void write(){
    assert(freopen("rucsac.out","w",stdout)!=NULL);
    printf("%d",sol);
}

int main(){
    read();
    solve();
    write();
    return 0;
}