Cod sursa(job #874636)

Utilizator pulseOvidiu Giorgi pulse Data 9 februarie 2013 09:28:21
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.5 kb
#include<fstream>
#define nmax 10002
using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");

int n,gmax,i,w[nmax],p[nmax],v[nmax];

int main()
{
    f>>n>>gmax;
    int sol=0;
    for(i=1;i<=n;i++){
    f>>w[i];
    f>>p[i];
    }
    v[0]=0;
    for(i=1;i<=n;i++)
    for(int j=gmax-w[i];j>=0;j--){
    if(v[j+w[i]]<v[j]+p[i])
    {
        v[j+w[i]]=v[j]+p[i];
        if(v[j+w[i]]>sol) sol=v[j+w[i]];
    }
    }
    g<<sol;
    f.close();g.close();
    return 0;
}