Cod sursa(job #2973928)

Utilizator 222cezarCezar Stilpeanu 222cezar Data 2 februarie 2023 20:25:33
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <bits/stdc++.h>

using namespace std;

bool ckmax(int &a, int b) { return (b > a ? a = b, 1 : 0); }

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

const int G = 10001;
vector<int> profit(G, -1);

int main() {
 	int n, g;
 	in >> n >> g;
	vector<int> w(n + 1), p(n + 1);
	for(int i = 1; i <= n; i++) {
		in >> w[i] >> p[i];
	}
	profit[0] = 0;
	for(int i = 1; i <= n; i++) {
	 	for(int j = g - w[i]; j >= 0; j--) {
	 	 	if(profit[j] != -1)
	 	 		ckmax(profit[j + w[i]], profit[j] + p[i]);
	 	}
	}
	int ans = *max_element(profit.begin(), profit.begin() + g + 1);

	out << ans << '\n';
}