Cod sursa(job #20195)

Utilizator anna_bozianuBozianu Ana anna_bozianu Data 20 februarie 2007 20:27:26
Problema Energii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.61 kb
#include<stdio.h>
long int g,w,c,e,p[5000],i;
int main()
{
	FILE *f=fopen("energii.cpp","r");
	fscanf(f,"%d%d",&g,&w);
	p[w]=1000000000;
	for(i=1;i<=g;i++)
	{fscanf(f,"%ld%ld",&e,&c);
	  if(e>=w) if(c<p[w]) p[w]=c;
	  else { for(i=w-e;i<w;i++)
		 if(p[i]) if(p[i]+c<p[w]) p[w]=p[i]+c;
		 for(i=w-e-1;i<=1;i--)
		 if(p[i]) {if(p[i+e]==0) p[i+e]=p[i]+c;
			   else if(p[i]+c<p[i+e]) p[i+e]=p[i]+c;
			   }
		 if(p[e]==0)p[e]=c;
		 else if(p[e]<c) p[e]=c;
		}
	}
	fclose(f);
	f=fopen("energii.out","w");
	if(p[w]==1000000000) fprintf(f,"-1\n");
	else fprintf(f,"%ld",p[w]);
	fclose(f);
	return 0;
}