Pagini recente » Cod sursa (job #2941417) | Rating moisescu (maestrul) | Cod sursa (job #2375346) | Cod sursa (job #1798963) | Cod sursa (job #2608779)
#include <fstream>
#include <algorithm>
std::ifstream in("rucsac.in");
std::ofstream out("rucsac.out");
int profits[10005];
int main()
{
int numberObjects;
int maxWeight;
int currentWeight;
int currentProfit;
int maximum = 0;
in >> numberObjects >> maxWeight;
/* Considering void solution set as valid */
profits[0] = 1;
for (int i = 0; i < numberObjects; i++)
{
in >> currentWeight >> currentProfit;
/* Increasing order would lead to updating the same profits multiple times */
for (int weight = maxWeight; weight >= currentWeight; weight--)
{
if (profits[weight - currentWeight] > 0)
{
profits[weight] = std::max(profits[weight - currentWeight] + currentProfit, profits[weight]);
}
}
/* Older solutions (with weight < currentWeight) are kept updated, so we don't have to copy them */
}
for (int j = maxWeight; j >= 1; j--)
{
maximum = std::max(profits[j], maximum);
}
/* As the void solution has weight 1, we have to decrease the optimal one */
out << maximum - 1;
return 0;
}