Cod sursa(job #3375666)

Utilizator horia3hHoria Bosoanca horia3h Data 12 noiembrie 2026 21:15:24
Problema Problema rucsacului Scor 50
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <stdio.h>

#define N 5000
#define G 10000
#define max(a,b) (a > b ? a : b)
int profit[N][G];

int main() {
	int n, gmax;
	FILE* in = fopen("rucsac.in", "r");
	fscanf(in, "%d %d", &n, &gmax);
	for (int i = 0; i < n; i++) {
		int g_i, p_i;
		fscanf(in, "%d %d", &g_i, &p_i);
		if (i == 0) {
			if (g_i <= gmax) profit[0][g_i] = p_i;
			continue;
		}
		for (int j = 1; j <= gmax; j++) {
			if (j >= g_i) {
				profit[i][j] = max(profit[i-1][j], profit[i-1][j-g_i]+p_i);
			} else {
				profit[i][j] = profit[i-1][j];
			}
		}
	}
	fclose(in);
	FILE* out = fopen("rucsac.out", "w");
	int profit_max = 0;
	for (int j = 1; j <= gmax; j++) {
		profit_max = max(profit_max, profit[n-1][j]);
	}
	fprintf(out, "%d", profit_max);
	fclose(out);
	return 0;
}