Cod sursa(job #218908)

Utilizator alex3el_n2oAlex Vladescu alex3el_n2o Data 3 noiembrie 2008 22:04:34
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.59 kb
#include <stdio.h>
int e[2000],c[2000];
long g,w,rez,i,t=0,S,s[10005],max,j;
int main()
{
freopen("energii.in","r",stdin);
freopen("energii.out","w",stdout);
scanf("%ld%ld",&g,&w);
for (i=1;i<=g;i++)
	scanf("%d %d",&e[i],&c[i]);
s[0]=1;
max=0;
for (i=1;i<=g;i++)
	{
	for (j=w;j>=0;j--)
		if (s[j])
		if ((s[j+e[i]]&&s[j+e[i]]>s[j]+c[i])||!s[j+e[i]])
			{
			s[j+e[i]]=s[j]+c[i];
			if (j+e[i]>max) max=j+e[i];
			}
	t+=e[i];
	}
rez=2000000000;
for (i=w;i<=max;i++)
	if (rez>s[i]&&s[i]>0)  rez=s[i];
if (rez==2000000000) printf("-1\n");
else printf("%ld\n",rez-1);
return 0;
}