Cod sursa(job #30825)

Utilizator razvi9Jurca Razvan razvi9 Data 15 martie 2007 10:26:46
Problema Energii Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.47 kb
#include<stdio.h>
int n,w,wmax,e,i,j,g;
long a[30000],s;
int main()
{freopen("energii.in","r",stdin);
 freopen("energii.out","w",stdout);
 scanf("%d %d",&n,&w);
 for(i=1;i<=n;i++)
 {scanf("%d %d",&e,&g);
  for(j=wmax;j>=0;j--)
   if((a[j]||j==0)&&a[j+e]==0||a[j+e]>a[j]+g) {a[j+e]=a[j]+g;
    if(j+e>wmax&&wmax<30000) wmax=j+e;}}
 if(wmax>w) s=2000000000;
 else s=-1;
 for(i=w;i<=wmax;i++)
 if(a[i]&&a[i]<s) s=a[i];
 printf("%ld",s);
 fclose(stdout);
 return 0;}