program window;
var A,B,MAXI,MINI:array[0..1000001] of longint;
first,n,k,i,j,m,t,min,max,maxh,minh:longint;
begin
assign(input,'window.in'); reset(input);
assign(output,'window.out'); rewrite(output);
readln(n,k);
read(first);
min:=first; minh:=1;
max:=first; maxh:=1;
A[1]:=first; B[1]:=1; //B[i]记录数组A中的数在原数组中的位置
for i:=2 to k do
begin
read(t);
A[i]:=t; B[i]:=i;
if min>t then begin min:=t; minh:=i; end;
if max<t then begin max:=t; maxh:=i; end;
end;
A[0]:=A[k]; B[0]:=B[k];
MINI[1]:=min; MAXI[1]:=max; //记录下区间最值
m:=1;
for i:=k+1 to n do
begin
read(t);
A[i mod k]:=t; B[i mod k]:=i;
if min>t then begin min:=t; minh:=i; end;
if max<t then begin max:=t; maxh:=i; end;
if minh<=i-k then
begin //维护
min:=A[0]; minh:=B[0];
for j:=1 to k-1 do
if min>A[j] then begin min:=A[j]; minh:=B[j]; end;
end;
if maxh<=i-k then
begin
max:=A[0]; maxh:=B[0];
for j:=1 to k-1 do
if max<A[j] then begin max:=A[j]; maxh:=B[j]; end;
end;
inc(m);
MINI[m]:=min;
MAXI[m]:=max;
end;
for i:=1 to m-1 do write(MINI[i],' '); writeln(MINI[m]);
for i:=1 to m-1 do write(MAXI[i],' '); writeln(MAXI[m]);
close(input); close(output);
end.