Pagini recente » Cod sursa (job #311977) | Cod sursa (job #683877) | Cod sursa (job #2494871) | Cod sursa (job #433917) | Cod sursa (job #3277458)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("rucsac.in");
ofstream g ("rucsac.out");
const int GMAX = 10001;
int maxProfit[GMAX]; ///maxProfit[i] - profitul maxim pe care putem sa il obtinem avand greutatea i
int main()
{
int n, gMax, ans = 0;
f >> n >> gMax;
for (int i = 1; i <= n; i++)
{
int weight, profit;
f >> weight >> profit;
for (int w = gMax; w >= weight; w--)
{
int crtProfit = maxProfit[w - weight] + profit;///vedem ce am obtine daca adaugam obiectul curent in rucsac
maxProfit[w] = max (maxProfit[w], crtProfit);
ans = max (ans, maxProfit[w]);
}
}
g << ans;
return 0;
}