Cod sursa(job #3316277)

Utilizator EnnBruhEne Andrei EnnBruh Data 18 octombrie 2025 09:33:29
Problema Problema rucsacului Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <bits/stdc++.h>
using namespace std;

string fileName = "rucsac";
ifstream in (fileName + ".in");
ofstream out (fileName + ".out");

const int nMax = 5000;
const int mMax = 10000;

int weight[nMax], profit[nMax];
int dp[nMax];
int main( ) {
	int n, maxWeight; in >> n >> maxWeight;
	for (int i = 0; i < n; ++i) 
		in >> weight[i] >> profit[i];

	int ans = 0;
	dp[0] = 0;

	for (int i = 0; i < n; ++i)
		for (int j = maxWeight - weight[i]; j >= 0; --j)
			if (dp[j + weight[i]] < dp[j] + profit[i]) {
				dp[j + weight[i]] = dp[j] + profit[i];
				ans = max(ans, dp[j + weight[i]]);
			}
	out << ans << '\n';
	return 0;
}