Cod sursa(job #148638)

Utilizator anca_tutescuTutescu Anca anca_tutescu Data 4 martie 2008 17:15:10
Problema Energii Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.59 kb
var f,h:text;
    c,e:array[1..1000] of integer;
    v:array[0..5000] of longint;
    i,j,g,w:integer;

begin
assign(f,'energii.in'); reset(f);
assign(h,'energii.out'); rewrite(h);
readln(f,g);
readln(f,w);
for i:=1 to g do
readln(f,e[i],c[i]);
v[0]:=0;
for i:=1 to w do
v[i]:=maxlongint;


for j:=1 to g do
for i:=w downto 0 do
   if v[i]<>maxint then
   if (i+e[j]>w) and (v[w]=maxint) then
          v[w]:=v[w]+c[j]
       else if v[i+e[j]]>v[i]+c[j] then
          v[i+e[j]]:=v[i]+c[j];
if (v[w]<>maxint) then
write(h,v[w])
else write(h,-1);
close(f); close(h);
end.