Pagini recente » Cod sursa (job #278571) | Cod sursa (job #1558411) | Cod sursa (job #1413698) | Cod sursa (job #947670) | Cod sursa (job #1845655)
#include<fstream>
using namespace std;
ifstream f("rucsac.in");
ofstream q("rucsac.out");
int p[5001],g[5001],prof[10001];
int n,k;
int main()
{
int i,j, maxx = 0;
f>>n>>k;
for(i=1;i<=n;i++)
f>>g[i]>>p[i];
for(i=1;i<=k;i++)
prof[i]=-1;
prof[0]=0;
for(i=1;i<=n;i++)
for(j=k-g[i];j>=0;j--)
{
if(prof[j]!=-1)
if(prof[j] + p[i] > prof[j+g[i]])
prof[j+g[i]] = prof[j] + p[i];
}
for(i=1;i<=k;i++)
if(prof[i] > maxx)
maxx = prof[i];
q<<maxx;
return 0;
}