Pagini recente » Cod sursa (job #1909433) | Cod sursa (job #2310118) | Cod sursa (job #2616408) | Cod sursa (job #3206851) | Cod sursa (job #3042161)
#include <fstream>
#include <vector>
using namespace std;
int main()
{
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, g;
vector<int> weights;
vector<int> profits;
fin >> n >> g;
vector<int> bestProfits(g + 1, 0);
for (auto i = 0; i < n; i++)
{
int w, p;
fin >> w >> p;
weights.push_back(w);
profits.push_back(p);
}
auto solution = -1;
for (auto i = 0; i < n; i++)
{
auto weight = weights[i];
for (auto j = g - weight; j >= 0; j--)
{
bestProfits[j + weight] = max(bestProfits[j + weight], bestProfits[j] + profits[i]);
solution = max(solution, bestProfits[j + weight]);
}
}
fout << solution;
fin.close();
fout.close();
return 0;
}