Pagini recente » Cod sursa (job #2627137) | Cod sursa (job #2700274) | Cod sursa (job #2127899) | Cod sursa (job #2702715) | Cod sursa (job #2121326)
#include <fstream>
#include <vector>
using namespace std;
int main()
{
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int objects, capacity;
fin >> objects >> capacity;
vector<int> profit(capacity + 1, 0);
int res = 0;
for (int i = 0; i < objects; ++i) {
int weight, value;
fin >> weight >> value;
for (int j = capacity - weight; j >= 0; --j) {
if (profit[j] > 0 || j == 0) {
auto new_profit = profit[j] + value;
auto new_weight = j + weight;
profit[new_weight] = max(profit[new_weight], new_profit);
res = max(res, new_profit);
}
}
}
fout << res << "\n";
return 0;
}