Cod sursa(job #391626)

Utilizator pykhNeagoe Alexandru pykh Data 5 februarie 2010 23:17:03
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include<stdio.h>
const char in[]="energii.in";
const char out[]="energii.out";
const int INF=2e8;
int v[5005], c[1005], e[1005], g, w, i, j;

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