Cod sursa(job #2175907)

Utilizator dragosmdvMoldovan Dragos dragosmdv Data 16 martie 2018 19:52:45
Problema Problema rucsacului Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int n, G, greu, profit, v[10001],maxi=-999999999;
int main()
{
    fin>>n>>G;
    for(int i=1;i<=n;i++)
    {fin>>greu>>profit;

        for(int i=G;i>=0;i--)
        {
            if((v[i]!=0 && i+greu<=G)|| i==0)
            {
                v[i+greu]=max(v[i+greu], v[i]+profit);

            }
            if(v[i+greu]>maxi)
                maxi=v[i+greu];
        }

    }

    fout<<maxi;
//for(int i=0;i<=G;i++)
//    cout<<v[i]<<" ";
    return 0;
}