Cod sursa(job #1275189)

Utilizator costty94Duica Costinel costty94 Data 24 noiembrie 2014 20:50:00
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <iostream>
#include <fstream>
#include <cstdio>

#define MIN(a, b) ((a < b) ? a : b)

using namespace std;
std::ifstream f("energii.in", std::ifstream::in);
std::ofstream g("energii.out", std::ofstream::out);


int n, w, eg[1010], cg[1010], d[10100], i, j;

int main()
{
	
	f >> n >> w;
	for (i = 1; i <= n; i++)
		f >> eg[i] >> cg[i];
	for (i = 1; i <= w; i++)
		d[i] = 1 << 30;
	d[0] = 0;
	for (i = 1; i <= n; i++)
	{
		for (j = w; j >= 0; j--)
		{
			if (d[j] != 1 << 30)
			{
				if (d[j] + cg[i] < d[j + eg[i]])
					d[j + eg[i]] = d[j] + cg[i];
			}
		}
	}
	if (d[w] == 1 << 30)
		g << "-1";
	else
		g << d[w];
	return 0;
}