Cod sursa(job #1111617)

Utilizator valiro21Valentin Rosca valiro21 Data 18 februarie 2014 23:34:11
Problema Problema rucsacului Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <cstdio>

#define NMax 5002

using namespace std;

long mp[NMax];
long maxG, weight, price, g, n;

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

    scanf ( "%ld %ld", &n, &g );
    for ( long i = 1; i <= n; i++ ) {
        scanf ( "%ld %ld", &weight, &price );
        for ( long j = g - weight; j >= 0; j-- )
            if ( ( j == 0 || mp[j] ) && mp[j + weight] < mp[j] + price )
                mp[j + weight] = mp[j] + price;
    }

    for ( long i = 1; i <= g; i++ )
        if ( mp[i] > maxG )
            maxG = mp[i];

    printf ( "%ld\n", maxG );

    return 0;
}