Pagini recente » Cod sursa (job #430049) | Cod sursa (job #130185) | Istoria paginii runda/craciun-viteza-3 | Cod sursa (job #2155254) | Cod sursa (job #1698945)
#include <iostream>
#include <cstdio>
using namespace std;
int n,lim;
int p[5001],profit[10001],g[5001];
int main()
{
freopen("rucsac.in","r",stdin);
freopen("rucsac.out","w",stdout);
cin>>n>>lim;
int i,j;
for(i=1; i<=n; i++)
cin>>g[i]>>p[i];
for(j=1; j<=lim; j++)
profit[j]=-1;
profit[0]=0;
for(i=1; i<=n; i++)
for(j=lim-g[i]; j>=0; j--)
if(profit[j]!=-1 and profit[j]+p[i]>profit[j+g[i]])
profit[j+g[i]]=profit[j]+p[i];
int maxi=0;
for(i=1; i<=lim; i++)
if(maxi<profit[i])
maxi=profit[i];
cout<<maxi;
return 0;
}