Cod sursa(job #1553307)

Utilizator sulzandreiandrei sulzandrei Data 19 decembrie 2015 16:06:29
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int main()
{
    int n,G;

    in >> n >> G;
    int     w[n+1],
            p[n+1],
    //cost[n+1][G+1],
    cost2[2][G+1];
    for(int i = 1 ; i <= n ; i ++)
        in >> w[i] >> p[i];
    for(int g = 0 ; g <= G ; g++)
       // cost[0][g] = 0,
        cost2[0][g] = 0;
    for(int i = 1 ; i <= n ; i ++)
    {
        //cost[i][0] = 0;
        cost2[1][0] = 0;
        for(int g = 1 ; g <= G ; g ++)
            if (w[i]<= g)
                if ( p[i] + cost2[0][g-w[i]] > cost2[0][g])
                    cost2[1][g] = p[i] + cost2[0][g-w[i]];
                else
                    cost2[1][g] = cost2[0][g];
            else
                cost2[1][g] = cost2[0][g];
        for(int g = 1 ; g <= G ; g ++)
            cost2[0][g] = cost2[1][g];
    }
    out<<cost2[1][G];

    return 0;
}