Cod sursa(job #407366)

Utilizator BaduBadu Badu Badu Data 2 martie 2010 11:42:06
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.64 kb
#include<fstream>
#include<cstring>
using namespace std;

int w,ge;
int G[1002];
int E[1002];
int S[5002];

inline int Min( int a, int b ) { return ( a<b?a:b); }

int main(){
	
	ifstream f("energii.in");
	ofstream g("energii.out");
	
	f>>ge>>w;
	int i,j;
	for(i=1;i<=ge;i++) f>>E[i]>>G[i];
	
	for( i=1;i<=w;i++)S[i]=50000;
	
	for(i=1;i<=ge;i++)
		for(j=w;j>=0;j--)
			if( S[j] != 50000 ) 
			{
				if( j + E[i] < w )
				{
					if( S[j+E[i]] > S[j] + G[i] ) S[j+E[i]] = S[j]+G[i];
				}
				else
				{
					if ( S[j] + G[i] < S[w] ) S[w] = S[j] + G[i];
				}	
			}
			
			g<<(S[w]==50000?-1:S[w]);
	return 0;
}