Cod sursa(job #807568)

Utilizator danutbodbodnariuc danut danutbod Data 4 noiembrie 2012 22:33:31
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.47 kb
#include<fstream>
using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");
int n,gv,i,j,gr,c,a,b;
int mat[3][10010];
int main()
{
    f>>n>>gv;
    a=1;b=2;
    for(i=1;i<=n;i++)
    {
        f>>gr>>c;
        for(j=1;j<=gv;j++)
        {
            if(gr<=j) mat[b][j]=max(mat[a][j],mat[a][j-gr]+c);
            else mat[b][j]=mat[a][j];
        }
        swap(a,b);
    }
    g<<mat[a][gv]<<'\n';
    f.close();g.close();
    return 0;
}