Cod sursa(job #2007793)

Utilizator shantih1Alex S Hill shantih1 Data 4 august 2017 01:14:33
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include <iostream>
#include <fstream>

using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");

int i, cw, n, g, a, b, x, y, rc[3][10004], mx;

int main () {
    
    fin >> n >> g;
    
    x = 1;  y = 2;
    for (i = 1; i <= n; i++)
    {
        fin >> a >> b;
    
        for (cw = 0; cw <= g; cw++)
        {
            rc[y][cw] = rc[x][cw];
            
            if (a <= cw)
                rc[y][cw] = max(rc[y][cw], rc[x][cw-a]+b);
        }
        
        a = x;
        x = y;
        y = a;
    }
    
    fout << rc[x][g] << "\n";
}