Cod sursa(job #2232546)

Utilizator DRLDRLRaul Ronald Galea DRLDRL Data 19 august 2018 22:27:39
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#pragma once
#include<iostream>
#include<algorithm>
#include<fstream>

using namespace std;

ifstream fin("energii.in");
ofstream fout("energii.out");

#define NRG 1001
#define Necesar 5001


int energieGenerata[NRG], cost[NRG], lCurent[Necesar], lPrecedent[Necesar], inf = 1<<29;

int main() {
	int nrGen, Energie, Emax = 0;
	fin >> nrGen >> Energie;

	for (int i = 1; i <= nrGen; i++)
		fin >> energieGenerata[i] >> cost[i];

	for (int j = 0; j <= Energie; j++) {
		lPrecedent[j] = lCurent[j] = inf;
	}

	for (int i = 1; i <= nrGen; i++)
	{
		for (int j = 0; j <= Energie; j++) {
			if (energieGenerata[i] >= j)
				lCurent[j] = min(lPrecedent[j], cost[i]);
			else
				lCurent[j] = min(lPrecedent[j], lPrecedent[j - energieGenerata[i]] + cost[i]);
		}
		for (int j = 0; j <= Energie; j++)
			lPrecedent[j] = lCurent[j];
	}
	if (lCurent[Energie] != inf)
		fout << lCurent[Energie]; // costul minim de produs Energie avand la dispozitie toate elementele;
	else
		fout << -1;
	return 0;
}