Cod sursa(job #59137)

Utilizator gozmanGrosu Andrei Nicolae gozman Data 8 mai 2007 10:36:21
Problema Energii Scor 5
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.05 kb
program energii;
var en,c,c1:array[0..1001] of word;
    s:array[0..1001] of boolean;
    g,n,i,j,w,vmax,cost:word;
    f:text;
    begin
    assign(f,'energii.in'); reset(f);
    readln(f,g); readln(f,w);
    for i:= 1 to g do
    readln(f,en[i],c[i]);
    close(f);
    vmax:=0;
    s[0]:=false;
    c1[0]:=0;
    cost:=65000;
    assign(f,'energii.out'); rewrite(f);
    for i:=1 to g do
    for j:=vmax downto 0 do
    if not (s[j]) then
       begin
          if s[j+en[i]] then
                        begin
                             if j+en[i]>vmax then vmax:=j+en[i];
                             s[j+en[i]]:=false;
                             c1[j+en[i]]:=c1[j]+c[i];
                       end
                       else
                         if c1[j+en[i]]<c1[j]+c[i] then
                                                   c1[j+en[i]]:=c1[j]+c[i];
       if j+en[i]>=w then if cost>c1[j+en[i]] then cost:=c1[j+en[i]];
       end;
   if cost=65000 then writeln(f,'-1')
                  else writeln(f,cost);
  close(f);
  end.