Cod sursa(job #2568938)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 4 martie 2020 10:31:04
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda r3capitusulare Marime 0.47 kb
#include <bits/stdc++.h>

using namespace std;

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

const int DIM = 1e4 + 1;

int best[DIM];

main()
{
	int n, w;
	fin >> n >> w;
	
	for(int i = 1; i <= n; i++)
	{
		int x, y;
		fin >> x >> y;
		
		for(int 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(int i = 1; i <= w; ++i)
		ans = max(ans, best[i]);
	
	fout << ans << '\n';
}