Cod sursa(job #1647171)

Utilizator Andrew07Cosmin Alex Andrew07 Data 10 martie 2016 19:13:52
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb

#include <fstream>
using namespace std;
ifstream f("rucsac.in");
ofstream fout("rucsac.out");
struct gigi{ long long greutate ;long long profit;} v[10000];
long long i , b[10000];
int main()
{
    int n , g ;
    long maxx=0;
    f>>n>>g;
    for(i=1;i<=n;i++)
    {
        f>>v[i].greutate>>v[i].profit;
    }
    for(i=1;i<=n;i++)

    {
        for(int  j=g ;j>=0;j--)
         {
         if(j-v[i].greutate!=0)
        {
            if(b[j-v[i].greutate] && b[j-v[i].greutate]+v[i].profit>b[j])
                {b[j]=b[j-v[i].greutate]+v[i].profit;
                 if(b[j]>maxx) maxx=b[j];
            }

        }
        else
        {

            b[j]=v[i].profit;
        }

         }
    }
    fout<<maxx;

}