Cod sursa(job #900388)

Utilizator caliuxSegarceanu Calin caliux Data 28 februarie 2013 19:26:53
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <stdio.h>
using namespace std;

int N, G, g[10002], p[10002], profit[10002], i, j;

int maxim(){
    int max =0;
    for(i = 1; i <= G; i++){
        if(profit[i] > max){
            max = profit[i];
        }
    }
    return max;
}

int main(){

    freopen("rucsac.in", "r", stdin);
    freopen("rucsac.out", "w", stdout);

    scanf("%d%d", &N, &G);
    for(i = 1; i <= N; i++){
        scanf("%d%d", &g[i], &p[i]);
    }

    for(i = 1; i <= N; i++){
        for(j = G - g[i]; j >= 1; j--){
            if(profit[j] != 0 && p[i] + profit[j] > profit[g[i] + j] ){
                profit[g[i] + j] = p[i] + profit[j];
            }
        }
        if(g[i] <= G && p[i] > profit[g[i]]){
            profit[g[i]] = p[i];
        }
    }
    printf("%d", maxim());
}