Pagini recente » Cod sursa (job #1199526) | Cod sursa (job #327976) | Cod sursa (job #2303103) | Cod sursa (job #1575284) | Cod sursa (job #2836486)
#include<fstream>
std :: ifstream in("rucsac.in");
std :: ofstream out("rucsac.out");
using namespace std;
int w[5001] , p[5001] , profit[10001];
int main()
{
int n , g;
in>>n>>g;
for(int i=0;i<n;i++)
in>>w[i]>>p[i] ;
for(int i=1;i<=g;i++)
profit[i]=-1;
profit[0]=0;
for(int i=0;i<n;i++)
for(int j=g-w[i];j>=0;j--)
if (profit[j]!=-1)
{
profit[j+w[i]]=max(profit[j]+p[i], profit[j+w[i]]);
}
int maxim=0;
for(int i=1;i<=g;i++)
{
if(maxim<profit[i])
maxim=profit[i];
}
out<<maxim;
}