Cod sursa(job #1698935)

Utilizator andrei_bB. Andrei andrei_b Data 5 mai 2016 17:57:44
Problema Problema rucsacului Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>

using namespace std;

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

const int Nmax=5002;
const int Gmax=10002;

int n,g,maxim=-1;
int w[Nmax],p[Nmax],profit[Nmax];


int main()
{
    fin>>n>>g;
    for ( int i=1 ; i<=n ; i++ )
        fin>>w[i]>>p[i];

    for ( int j=1 ; j<=g ; j++ )
        profit[j]=-1;
    profit[0]=0;

    for ( int i=1 ; i<=n ; i++ ){
        for ( int j=g-w[i] ; j>=0 ; j-- ){
            if ( profit[j] != -1 &&  profit[j] + p[i] > profit[j+w[i]]  )
                profit[j+w[i]]=profit[j]+p[i];
        }
    }
    for ( int i=1 ; i<=g ; i++ )
        if ( profit[i] > maxim )
            maxim=profit[i];

    fout<<maxim;


}