Pagini recente » Cod sursa (job #1577211) | Cod sursa (job #1742874) | Cod sursa (job #958152) | Cod sursa (job #1647932) | Cod sursa (job #2470557)
#include <bits/stdc++.h>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
const int MAX=10003;
const int maxi=5003;
int profit[MAX],p[maxi],g[maxi],n,G,pmax=-1;
int main()
{
in>>n>>G;
for(int i=0;i<n;i++)
in>>g[i]>>p[i];
for(int j=1;j<=G;j++)
profit[j]=-1;
for(int i=0;i<n;i++)
{
for(int j=G-g[i];j>=0;j--)
if(profit[j]!=-1 && profit[j]+p[i]>profit[j+g[i]])
{
profit[j+g[i]]=profit[j]+p[i];
}
}
for(int i=1;i<=G;i++)
pmax=max(profit[i],pmax);
out<<pmax;
return 0;
}