Cod sursa(job #1435811)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 14 mai 2015 17:09:26
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <fstream>
#include <vector>
#include <utility>
using namespace std;

constexpr int inf = (1<<30);

void fa_trecere_o_data(vector<int>& v, const int g, const int e, const int c, int& rez){
	const int min1 = max(g-e, 0);
	for(int j = g-1; j >= min1; --j){
		if(v[j] != inf){
			rez = min(rez, v[j]+c); } }
	for(int j = g-e-1; j >= 0; --j){
		if(v[j] != inf && v[j]+c < v[j+e]){
			v[j+e] = v[j]+c; } } }

int main(){
	ifstream in("energii.in");
	ofstream out("energii.out");
	int n = 0, g = 0;
	in >> n >> g;
	vector<int> v(g, inf);
	v[0] = 0;
	int rez = inf;
	for(int i = 0, e = 0, c = 0; i < n; ++i){
		in >> e >> c;
		fa_trecere_o_data(v, g, e, c, rez); }
	out << (rez == inf ? -1 : rez);
	return 0; }