Cod sursa(job #1212298)

Utilizator IonSebastianIon Sebastian IonSebastian Data 24 iulie 2014 12:39:44
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
#include <cstring>

using namespace std;

ifstream in("rucsac.in");
ofstream out("rucsac.out");

const int MAX_N = 5000, MAX_G = 10000;

int act[MAX_G+1], ant[MAX_G+1];
int w[MAX_N+1], p[MAX_N+1];

int maxim(int a, int b)
{
    return (a > b) ? a:b;
}

int main()
{
    int n, g, i, j;
    in >> n >> g;
    for(i = 1; i <= n; i++)
    {
        in >> w[i] >> p[i];
    }

    for(i = 1; i <= n; i++)
    {
        for(j = 0; j <= g; j++)
        {
            if(j-w[i] >= 0)
            {
                act[j] = maxim(ant[j], ant[j-w[i]]+p[i]);
            } else {
                act[j] = ant[j];
            }
        }
        memcpy(ant,act,4*(g+1));
    }
    out << act[g];
    return 0;
}