Cod sursa(job #719462)

Utilizator andreiseiceanSeicean Andrei andreiseicean Data 21 martie 2012 20:14:36
Problema Energii Scor 5
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.58 kb
var n,i,j,w,x,x1,y:integer;
    f,g:text;
    a:array[0..1001,0..10001]of longint;
begin
assign(f,'energii.in');reset(f);assign(g,'energii.out');rewrite(g);
read(f,n,w);
for i:=1 to n do
  begin
     read(f,x,y);
     if (y<a[i-1,x])or(a[i-1,x]=0) then a[i,x]:=y else a[i,x]:=a[i-1,x];
     for j:=1 to w do
        if a[i-1,j]<>0 then
          if (a[i-1,j+x]>a[i-1,j]+y)or(a[i-1,j+x]=0) then a[i,j+x]:=a[i-1,j]+y
                                                     else a[i,j+x]:=a[i-1,j+x];
  end;
if a[n,w]<>0 then write(g,a[n,w]) else write(g,-1);
close(f);close(g);
end.