Pagini recente » Cod sursa (job #3191184) | Cod sursa (job #2137262) | Cod sursa (job #1569803) | Cod sursa (job #2007172) | Cod sursa (job #1129737)
#include <iostream>
#include <fstream>
using namespace std;
int main()
{ int n,i,g,j,maxx;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
fin>>n>>g;
int gr[5005],p[5005],profit[10005];
for(i=0;i<n;i++)
{fin>>gr[i];
fin>>p[i];}
for(i=0;i<=g;i++)
profit[i]=-1;
profit[0]=0;
for(i=0;i<=n;i++)
{for(j=g-gr[i];j>=0;j--)
{
if((profit[j]!=-1) and (profit[j]+p[i]>profit[j+gr[i]]))
profit[j+gr[i]]=profit[j]+p[i];
}}
maxx=profit[0];
for(i=0;i<=g;i++)
if(maxx<profit[i])
maxx=profit[i];
fout<<maxx;
fin.close();fout.close();
return 0;
}