Pagini recente » Cod sursa (job #1960629) | Cod sursa (job #3041966) | Cod sursa (job #2868163) | Cod sursa (job #435865) | Cod sursa (job #2971195)
#include <fstream>
using namespace std;
const int N = 5000, G = 10000;
struct obiect{
int w, p;
};
int main()
{
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int n, g, profit[G+1], pmax = -1;
obiect x[N];
in >> n >> g;
for(int i = 0; i < n; i++)
in >> x[i].w >> x[i].p;
for(int j = 1; j <= g; j++)
profit[j] = -1;
profit[0] = 0;
for(int i = 0; i < n; i++)
{
for(int j = g - x[i].w; j >= 0; j--)
{
if(profit[j] != -1 && profit[j] + x[i].p > profit[j + x[i].w])
profit[j + x[i].w] = profit[j] + x[i].p;
}
}
for(int i = 0; i < g; i++)
pmax = max(profit[i], profit[i+1]);
out << pmax;
in.close();
out.close();
return 0;
}