Cod sursa(job #197212)

Utilizator tamas_iuliaTamas Iulia tamas_iulia Data 2 iulie 2008 20:49:30
Problema Energii Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.41 kb
type gen = record
  e,c : longint;
  end;
var v : array[1..2001] of gen;
    cost,ex : array[0..16001] of longint;
    n,w,i,j,cmin,ok : longint;
    f,g : text;
begin
  assign(f,'energii.in');reset(f);
  assign(g,'energii.out');rewrite(g);
  read(f,n,w);
    for i:=1 to n do
      read(f,v[i].e,v[i].c);

  cmin:=maxlongint;
  ex[0]:=1; cost[0]:=0; ok:=0;
    if w<=0 then
    begin
      cmin:=0; ok:=1;
    end;
    for i:=1 to n do
      for j:=w-1 downto 0 do
        if ex[j]=1 then
        begin
            if ex[j+v[i].e]=0 then
            begin
              cost[j+v[i].e]:=cost[j]+v[i].c;
              ex[j+v[i].e]:=1;
                if j+v[i].e>=w then
                  if cost[j+v[i].e]<cmin then
                  begin
                    cmin:=cost[j+v[i].e];
                    ok:=1;
                  end
            end
            else
              if cost[j+v[i].e]>cost[j]+v[i].c then
              begin
                cost[j+v[i].e]:=cost[j]+v[i].c;
                  if j+v[i].e>=w then
                    if cost[j+v[i].e]<cmin then
                    begin
                      cmin:=cost[j+v[i].e];
                      ok:=1;
                    end;
              end;
      end;

    if ok=0 then writeln(g,'-1')
    else writeln(g,cmin);
    close(g);
end.