Pagini recente » Cod sursa (job #157655) | Cod sursa (job #3237383) | Cod sursa (job #98091) | Cod sursa (job #3240010) | Cod sursa (job #3209155)
#include <bits/stdc++.h>
struct obj {
int weight;
int profit;
};
int main()
{
int n, W;
int max_profit = 0;
std::vector<struct obj> objs;
std::vector<int> total_profit;
std::ifstream fin("rucsac.in");
fin >> n >> W;
objs.resize(n);
total_profit.resize(W);
for (int w, p, i = 0; i < n; ++i) {
fin >> w >> p;
objs[i].weight = w;
objs[i].profit = p;
}
fin.close();
for (int i = 0; i < n; ++i)
for (int w = W - objs[i].weight; w >= 0; --w)
if (total_profit[w + objs[i].weight] < total_profit[w] + objs[i].profit) {
total_profit[w + objs[i].weight] = total_profit[w] + objs[i].profit;
max_profit = std::max(max_profit, total_profit[w + objs[i].weight]);
}
std::ofstream fout("rucsac.out");
fout << max_profit << '\n';
fout.close();
return 0;
}