Cod sursa(job #920426)

Utilizator teodor98Teodor Sz teodor98 Data 20 martie 2013 13:08:10
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <iostream>
#include <fstream>
using namespace std;

#define N 10000
ifstream in("rucsac.in");
ofstream out("rucsac.out");
long n,k,profit[N],g[N],p[N],pmax,i,j;

void citire()
{
in >> n >>k;
for(i =1;i<=n;i++)
    in >> g[i] >> p[i];
}
void verificare()
{
    for(i=1;i<=n;i++)
    {
            for(j = k-g[i];j>0;j--)
            {


            if(profit[j]!=0 && (profit[j] + p[i] >profit[j+g[i]]))
                        profit[j+g[i]] = profit[j]+p[i];
            if(profit[j+g[i]] > pmax)
                pmax = profit[j+g[i]];

            }
              if(g[i] <=k && p[i] > profit[g[i]])
                profit[g[i]] = p[i];
            pmax = max(pmax, profit[g[i]]);

    }

}
void scrie()
{
    out << pmax;
}
int main()
{
    citire();
    verificare();
    scrie();
    return 0;
}