Cod sursa(job #2785077)

Utilizator BarbuceanuConstantinBarbuceanu Constantin BarbuceanuConstantin Data 17 octombrie 2021 22:20:07
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 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 - weights[i]; j >= 0; --j) {
			if (max_profits[j + weights[i]] < max_profits[j] + profits[i]) {
				max_profits[j + weights[i]] = max_profits[j] + profits[i];
			}
		}
	printf("%d", max_profits[greutate_maxima]);

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