Pagini recente » Cod sursa (job #1377987) | Cod sursa (job #2807679) | Cod sursa (job #963853) | Cod sursa (job #2350513) | Cod sursa (job #2777831)
#include <iostream>
#include <fstream>
#define dim 50001
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int w[dim],p[dim],profit[2*dim];
int main()
{
int n, g;
in >> n >> g;
for(int i = 0; i < n; i++)
{
in >> w[i] >> p[i];
}
profit[0] = 0;
for (int i = 1; i <= g; i++) {
profit[i] = -1;
}
for(int i = 0; i < n; i++)
{
for(int j = g; j >= w[i]; j--)
{
if(profit[j-w[i]] != -1 && profit[j-w[i]] + p[i] > profit[j]) {
profit[j] = profit[j-w[i]] + p[i];
}
}
}
int profit_maxim = profit[0];
for (int i = 1; i <= g; i++) {
if (profit[i] > profit_maxim) {
profit_maxim = profit[i];
}
}
out << profit_maxim << '\n';
return 0;
}