Pagini recente » Cod sursa (job #1863641) | Cod sursa (job #969786) | Cod sursa (job #794482) | Cod sursa (job #869176) | Cod sursa (job #1814739)
#include <fstream>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int main()
{
int profit[10001],g[10001],p[10001],i,j,n,k,maxx;
in>>n>>k;
for(i=1; i<=n; i++)
in>>g[i]>>p[i];
for(i=1; i<=k; i++)
profit[i]=-1;
profit[0]=0;
for(i=1; i<=n; i++)
for(j=k-g[i]; j>=0; j--)
if(profit[j]!=-1) if(profit[j]+p[i]>profit[j+g[i]])
profit[j+g[i]]=profit[j]+p[i];
maxx=profit[1];
for(i=2;i<=k;i++)
if(profit[i]>maxx) maxx=profit[i];
out<<maxx;
return 0;
}