Cod sursa(job #800875)

Utilizator Sm3USmeu Rares Sm3U Data 22 octombrie 2012 20:36:37
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <cstdio>

#define nMax 5010
#define gMax 100010

#define max(a,b) ((a > b) ? a : b)

using namespace std;

struct obiecte{
    int g;
    int val;
}ob[nMax];

int a[gMax];
int b[gMax];
int n;
int g;

void citire(){
    scanf ("%d %d", &n, &g);
    for (int i = 1; i <= n; ++ i){
        scanf ("%d %d", &ob[i].g , &ob[i].val);
    }
}

void rucsac (){
    for (int i = 1; i <= n; ++ i){
        int j;
        for (j = 1; j < ob[i].g; ++ j){
            b[j] = max (b[j - 1], a[j]);
        }
        for (; j <= g; ++ j){
            b[j] = max (b[j - 1], a[j]);
            b[j] = max (b[j], ob[i].val + a[j - ob[i].g]);
        }
        for (j = 1; j <= g; ++ j){
            a[j] = b[j];
        }
    }
    printf ("%d\n", a[g]);
}


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

    citire();
    rucsac();

    return 0;
}