Cod sursa(job #146884)

Utilizator AndreiDDiaconeasa Andrei AndreiD Data 2 martie 2008 13:05:14
Problema Energii Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.59 kb
const max=100000000;
var n,w,x,y,m,i,j:longint;
a:array[0..6001] of longint;
f,g:text;
begin
assign(f,'energii.in');
reset(f);
assign(g,'energii.out');
rewrite(g);
readln(f,n,w);
for i:=1 to w do
a[i]:=max;
m:=max;
for i:=1 to n do begin
read(f,x,y);
if (x>=w) then begin
if y<m then m:=y;
end
          else begin
          for j:=w-x to w do
          if a[j]+y<m then m:=a[j]+y;
          for j:=w downto x do
          if a[j-x]+y<a[j] then a[j]:=a[j-x]+y;
                 end;
end;
if m<>max then writeln(g,m)
          else writeln(g,'-1');
close(f);
close(g);
end.