Cod sursa(job #2970627)

Utilizator gege42George Timus gege42 Data 25 ianuarie 2023 16:53:58
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
struct object{
	int p;
	int g;
};
object a[100000];
int profit[100000];
int main() {
	int n, g;
	in >> n >> g;
	for (int i = 1; i <= n; i++) {
		in >> a[i].g >> a[i].p;
	}
	for (int j = 1; j <= g; j++) {
		profit[j] = -1;
	}
	profit[0] = 0;
	for (int i = 0; i <= n; i++) {
		for (int j = g - a[i].g; j >= 0; j--) {
			if (profit[j] != -1 && profit[j] + a[i].p > profit[j + a[i].g]) {
				profit[j + a[i].g] = profit[j] + a[i].p;
			}
		}
	}
	int max = 0;
	for (int j = 1; j <= g; j++) {
		if (profit[j] > max) max = profit[j];
	}
	out << max;
}