Pagini recente » Cod sursa (job #729247) | Cod sursa (job #2050256) | Cod sursa (job #516952) | Cod sursa (job #91094) | Cod sursa (job #719093)
Cod sursa(job #719093)
var n,i,j,w,x,y,p,x1,aux:integer;
f,g:text;
a,b:array[0..5001]of longint;
begin
assign(f,'energii.in');reset(f);assign(g,'energii.out');rewrite(g);
read(f,n,w); p:=0;
for i:=1 to n do
begin
read(f,x,y);
if (y<a[x])or(a[x]=0) then begin a[x]:=y; p:=p+1; b[p]:=x; end;
j:=1;
while j<=p do
begin
aux:=x+b[j]; x1:=b[j];
if aux<=w then if a[aux]<>0 then begin if a[aux]>a[x1]+y then a[aux]:=a[x1]+y; end
else begin a[aux]:=a[x1]+y; p:=p+1; b[p]:=aux; end;
j:=j+1;
end;
end;
if a[w]<>0 then write(g,a[w]) else write(g,-1);
close(f);close(g);
end.