Cod sursa(job #490249)

Utilizator ChallengeMurtaza Alexandru Challenge Data 5 octombrie 2010 18:00:31
Problema Energii Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <fstream>

using namespace std;

const char InFile[]="energii.in";
const char OutFile[]="energii.out";
const int MaxN=5005;
const int MAX=1<<30;

ifstream fin(InFile);
ofstream fout(OutFile);

int W,CGi,EGi,G,sol,marked[MaxN],cost[MaxN];

int main()
{
	fin>>G>>W;
	cost[0]=0;
	marked[0]=1;
	for(register int i=0;i<G;++i)
	{
		fin>>EGi>>CGi;
		for(register int j=MaxN-1;j>=0;--j)
		{
			if(marked[j]==1)
			{
				int pos=j+EGi;
				if(j+EGi>W)
				{
					pos=W;
				}
				if(marked[pos]==1)
				{
					cost[pos]=min(cost[pos],cost[j]+CGi);
				}
				else
				{
					marked[pos]=1;
					cost[pos]=CGi+cost[j];
				}
			}
		}
	}
	fin.close();

	if(cost[W]==MAX)
	{
		cost[W]=-1;
	}
	fout<<cost[W];
	fout.close();
	return 0;
}