Cod sursa(job #19286)

Utilizator zepusSpuze Cristian-Iulius zepus Data 19 februarie 2007 09:27:42
Problema Energii Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.59 kb
#include <fstream.h>
long a[500000];
int main()
  {
   long w,n,x,y,i,max=0,j,bun;
   ifstream f("energii.in");
   ofstream g("energii.out");
   f>>n>>w;
   for (i=1;i<=n;i++)
     {
      f>>x>>y;
      for (j=0;j<=max;j++)
	{
	 if (a[j]!=0)
	   if (a[j+x]==0 || (a[j+x]!=0 && a[j]+y<a[j+x]))
	     { a[j+x]=a[j]+y; }
	}
      if (a[x]==0 || (a[x]!=0 && a[x]>y))
	a[x]=y;
      max=max+x;
     }

   bun=-1;
   for (i=w;i<=max;i++)
     if ((a[i]!=0 && bun==-1)||(a[i]!=0 && bun!=-1 && bun>a[i]))
       bun=a[i];
   g<<bun<<"\n";

   f.close();
   g.close();
   return 0;
  }