Cod sursa(job #1089536)

Utilizator robert_stefanRobert Stefan robert_stefan Data 21 ianuarie 2014 19:15:20
Problema Energii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#define IN "energii.in"
#define OUT "energii.out"
#define NMAX 1005
#define GMAX 5005
#define INFINIT 100000000

using namespace std;

ifstream in(IN);
ofstream out(OUT);

int n, energie, en[NMAX], cost[NMAX], vect1[GMAX], vect2[GMAX];

int main()
{
	int i, j;
	in>>n>>energie;
	for(i=1; i<=n; ++i)
		in>>en[i]>>cost[i];
	for(i=1; i<=energie; ++i)
		vect1[i]=INFINIT;
	for(i=1; i<=n; ++i)
	{
		for(j=1; j<=energie; ++j)
		{
			vect2[j]=vect1[j];
			if(en[i]<=j && cost[i] + vect2[j-en[i]] > vect2[j])
				vect2[j] = vect2[j-en[i]] + cost[i];
			if(en[i]>=j && cost[i] < vect2[j])
				vect2[j] = cost[i];
			/*if(en[i]<=j)
				if(vect1[j] < vect1[j-en[i]] + cost[i])
					vect2[j] = vect1[j-en[i]] + cost[i];
				else
					vect2[j] = vect1[j];
			else
				if(cost[i] < vect1[j])
					vect2[j] = cost[1];
				else
					vect2[j] = vect1[j];*/
		}
		for(j=1; j<=energie; ++j)
			vect1[j]=vect2[j];
	}
	if(vect1[energie]>=energie)
		out<<vect2[energie]<<'\n';
	else out<<"-1\n";
	in.close();
	out.close();
	return 0;
}