Cod sursa(job #2644132)

Utilizator akumariaPatrascanu Andra-Maria akumaria Data 23 august 2020 16:04:08
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <stdio.h>

int main(int argc, char ** args) {
	freopen("rucsac.in" , "r", stdin);
	freopen("rucsac.out", "w", stdout);

	int i, n, gmax, currentP, currentG, currentGMax, temporaryGMax;
	scanf("%d%d", &n, &gmax);
	int profits[gmax+1];

	for(i=0; i<=gmax; ++i)
		profits[i] = 0;
	profits[0] = 1;

	currentGMax = 0;
	for(i=0; i<n; ++i) {
		scanf("%d%d", &currentG, &currentP);
		temporaryGMax = currentGMax;
		for(int j = currentGMax; j>=0; --j) {
			if (profits[j] != 0)
				if (j + currentG <= gmax && profits[j] + currentP > profits[j + currentG]) {
					profits[j + currentG] = profits[j] + currentP;
					if (j + currentG > currentGMax)
						temporaryGMax = j + currentG;
				}
		}
		currentGMax = temporaryGMax;
	}

	temporaryGMax = 0;
	for(i=0; i<= gmax; ++i)
		if (profits[i] > temporaryGMax)
			temporaryGMax = profits[i];

	printf("%d\n", temporaryGMax - 1);
	return 0;

}