Cod sursa(job #3042161)

Utilizator Silviu.Stancioiu@gmail.comSilviu Stancioiu [email protected] Data 4 aprilie 2023 11:44:12
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#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;
}