Cod sursa(job #148559)

Utilizator anca_tutescuTutescu Anca anca_tutescu Data 4 martie 2008 15:37:00
Problema Energii Scor 5
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.57 kb
var f,h:text;
    c,e:array[1..1000] of word;
    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]:=maxint;
for i:=w downto 0 do
    if v[i]<>maxint then
       for j:=1 to g do
       if i+e[j]>w 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.