Pagini recente » Cod sursa (job #1433276) | Cod sursa (job #2389572) | Cod sursa (job #2961841) | Cod sursa (job #1232955) | Cod sursa (job #2468018)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in ("rucsac.in");
ofstream out ("rucsac.out");
int w[5002],p[5002];
long long profit[10002];
int main()
{
int n,g;
in>>n>>g;
for (int i=1;i<=n;i++)
{
in>>w[i]>>p[i];
}
for (int i=1;i<=g;i++)
profit[i]=-1;
for (int i=1;i<=n;i++)
{
for (int j=g-w[i];j>=0;j--)
{
if (profit[j]!=-1)
{
if (profit[j]+p[i]>profit[j+w[i]])
profit[j+w[i]]=profit[j]+p[i];
}
}
}
out<<profit[g];
return 0;
}