Cod sursa(job #490812)

Utilizator xtremespeedzeal xtreme Data 8 octombrie 2010 08:37:18
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <stdio.h>
#define Wmax 5000
#define INF 2000000000
#define Gmax 1000

int G, W, cost[2*Wmax+1], req[Gmax+1], gain[Gmax+1];

void read()
      {
	   int i;		
	   
	   freopen("energii.in","r",stdin);
	   
	   scanf("%d\n%d\n",&G,&W);
	   for(i=1;i<=G;i++)
		       scanf("%d %d",&gain[i],&req[i]);
	   
	   for(i=1;i<=2*W;i++)
				cost[i]=INF;
	   
	   fclose(stdin);
	  }
void solve()
	{
	 int i, j;	 
		
	 for(i=1;i<=G;i++)
		 for(j=1;j<=2*W;j++)
			    if(gain[i]<=j && cost[ j-gain[i] ]+req[i]<cost[j])
					  cost[j]=cost[ j-gain[i] ]+req[i];
	}
void write()
	{
	 int i, min;
		
	 freopen("energii.out","w",stdout);
	 
	 min=cost[W];
	 for(i=W+1;i<=2*W;i++)
		    if(cost[i]<min)
				 min=cost[i];
	 if(min==INF) 
		     printf("-1\n");
	 else 
		     printf("%d\n",min);
	 
	 fclose(stdin);
	}
int main()
	{
	 read();
	 solve();
	 write();
	 return 0;
	}