Cod sursa(job #403983)

Utilizator nandoLicker Nandor nando Data 25 februarie 2010 17:11:31
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include<stdio.h>

const int none=100000000;

int v[5005], c[1005], e[1005], g, w;

int main()
	{
		freopen("energii.in","r",stdin);
		freopen("energii.out","w",stdout);
		scanf("%d %d", &g, &w);
		for(int i=1;i<=g;++i){
			scanf("%d %d", &e[i], &c[i]);
		}
		for(int i=1;i<=w;++i){
			v[i]=none;
		}
		for(int i=1;i<=g;++i){
			for(int j=w;j>=1;--j)
				if(v[j]!=none)
					if(j+e[i]>=w)
						{if(v[j]+c[i]<v[w])v[w]=v[j]+c[i];}
					else if(v[j]+c[i]<v[j+e[i]])
						v[j+e[i]]=v[j]+c[i];
				if(e[i]>=w)
					{if(c[i]<v[w])v[w]=c[i];}
				else if(v[e[i]]>c[i])v[e[i]]=c[i];
		}
		if(v[w]==none){
			printf("-1\n");
		}else{
			printf("%d", v[w]);
		}
		return 0;
}