Cod sursa(job #60289)

Utilizator gozmanGrosu Andrei Nicolae gozman Data 13 mai 2007 15:31:31
Problema Energii Scor 5
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.23 kb
program energii;
type vec= array[1..1001] of longint;
var en,c:vec;
    s:array[0..10001] of boolean;
    c1:array[0..10001] of longint;
    g,i,j,w,vmax,cost,k: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);
   s[0]:=true; cost:=65000; vmax:=0;  c1[0]:=0;
   for j:= 1 to en[1] do  s[j]:=false;

   for i:= 1 to g do
   begin
   for j:=vmax downto 0 do
   if (s[j]) then
   begin
   if not(s[j+en[i]])then
   begin
        s[j+en[i]]:=true;
        c1[j+en[i]]:=c1[j]+c[i];
       if j+en[i]>=w then
       if cost>c1[j]+c[i] then cost:=c1[j]+c[i];
       if j+en[i]>vmax then vmax:=j+en[i];
   end
   else if c1[j+en[i]]>c1[j]+c[i] then
                                  begin
                                  c1[j+en[i]]:=c1[j]+c[i];
                                  if j+en[i]>=w then
                                  if cost>c1[j]+c[i] then cost:=c1[j]+c[i];
                                  end;
  end;
   for k:= vmax+1 to vmax+en[i+1] do s[k]:=false;
   end;
   assign(f,'energii.out'); rewrite(f);
   if cost=65000 then writeln(f,'-1')
                  else writeln(f,cost);
  close(f);
  end.