Cod sursa(job #2785073)

Utilizator BarbuceanuConstantinBarbuceanu Constantin BarbuceanuConstantin Data 17 octombrie 2021 22:11:01
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include<stdio.h>
#define N 5001
#define G 10001	
using namespace std;

int weights[N], profits[G];
int max_profits[G];

int max(int a, int b) {
	if(a > b) {
		return a;
	} else {
		return b;
	}
}

int main() {
	FILE *fp1, *fp2;
	fp1 = freopen("rucsac.in", "r", stdin);
	fp2 = freopen("rucsac.out", "w", stdout);
	int n, greutate_maxima, profit_ob_introdus, profit_ob_neintrodus;

	scanf("%d %d", &n, &greutate_maxima);
	for (int i = 1; i <= n; ++i) {
		scanf("%d %d", &weights[i], &profits[i]);
	}
	for (int i = 1; i <= n; ++i)
		for(int j = greutate_maxima; j - weights[i] >= 0; --j) {
			profit_ob_neintrodus = max_profits[j];
			profit_ob_introdus = max_profits[j - weights[i]] + profits[i];
			max_profits[j] = max(profit_ob_introdus, profit_ob_neintrodus);
		}
	printf("%d", max_profits[G]);

	fclose(fp1);
	fclose(fp2);
	return 0;
}