Pagini recente » Cod sursa (job #1446488) | Cod sursa (job #589754) | Cod sursa (job #724017) | Cod sursa (job #116014) | Cod sursa (job #3209154)
#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);
dp.resize(W + 1, std::vector<int>(n + 1, 0));
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 << dp[W][n] << '\n';
fout.close();
return 0;
}