Pagini recente » Cod sursa (job #2468888) | Cod sursa (job #2346542) | Cod sursa (job #475203) | Cod sursa (job #554488) | Cod sursa (job #2577256)
#include <algorithm>
#include <fstream>
#include <vector>
int main()
{
std::ifstream fin("rucsac.in");
std::ofstream fout("rucsac.out");
int n, capacity;
fin >> n >> capacity;
std::vector<int> profit(capacity + 1, -1);
profit[0] = 0;
for (int i = 0; i < n; i += 1) {
int weight, value;
fin >> weight >> value;
for (int c = capacity - weight; c >= 0; c -= 1) {
if (profit[c] >= 0) {
profit[c + weight] = std::max(profit[c + weight],
profit[c] + value);
}
}
}
fout << *max_element(profit.begin(), profit.end()) << "\n";
return 0;
}