Pagini recente » Cod sursa (job #2431542) | Cod sursa (job #3246847) | Cod sursa (job #2805732) | Cod sursa (job #669108) | Cod sursa (job #2472438)
#include <fstream>
using namespace std;
long long n,G,g[5006],p[5006],profit[5006];
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int main()
{
//profit[j]=profitul maxim care se poate obtine cu obiecte care au suma greutatilor j
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];
}
}
long long maxi=0;
for(int i=0;i<=G;i++) maxi=max(maxi,profit[i]);
out<<maxi;
return 0;
}