Cod sursa(job #1995478)

Utilizator FredyLup Lucia Fredy Data 28 iunie 2017 01:15:23
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.55 kb
#include <iostream>
#include <fstream>

using namespace std;

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

#define lim 10010
int n,g;
int v[lim];

int main()
{
    int x,y;

    fin>>n>>g;

    for(int i=1; i<=n; i++)
    {
        fin>>x>>y;
        for(int j=g-x; j; j--)
            if(v[j])
                v[j+x]=max(v[j+x],v[j]+y);
        v[x]=max(v[x],y);
    }

    int maxm=-1;
    for(int i=0; i<=g; i++)
        if(v[i]>maxm)   maxm=v[i];
    fout<<maxm;

    fin.close();
    fout.close();
    return 0;
}