Pagini recente » Borderou de evaluare (job #2470009) | Cod sursa (job #2470541)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
const int dim = 10005;
const int nmax = 5005;
int n,g,w[nmax],p[nmax],profit[dim];
int main()
{
in >> n >> g;
for (int i=0; i<n; i++)
{
in >> w[i] >> p[i];
}
for (int j=1; j<=g; j++)
{
profit[j] = -1;
}
for (int i=0; i<n; i++)
{
for (int j=g-w[i]; j>=0; j--)
{
if (profit[j] != -1 && profit[j] + p[i] > profit[j + w[i]])
{
profit[j + w[i]] = profit[j] + p[i];
}
}
}
int maxi = -1;
for (int j=1; j<=g; j++)
{
maxi = max(maxi , profit[j]);
}
out << maxi;
return 0;
}