Cod sursa(job #2571440)

Utilizator PatriciaCretoiuCretoiu Patricia PatriciaCretoiu Data 4 martie 2020 23:16:07
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda r3capitusulare Marime 0.47 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in("rucsac.in");
ofstream out("rucsac.out");

const int N = 1e4 + 4;
int n, w, i, j, x, y, best[N];

main()
{
	in >> n >> w;

	for(i = 1; i <= n; i++)
	{
		in >> x >> y;

		for(j = w - x; j >= 1; j--)
			if(best[j]) best[j + x] = max(best[j + x], best[j] + y);

		best[x] = max(best[x], y);
	}

	int ans = 0;

	for(i = 1; i <= w; ++i)
		ans = max(ans, best[i]);

	out << ans;

	return 0;
}