program window;
const
maxn=1000000;
var
q,h1,h2:array [1..maxn] of longint;
n,k,min,max,head,tail,sp:longint;
f1,f2:text;
procedure init;
var i:longint;
begin
assign(f1,'window.in'); reset(f1);
assign(f2,'window.out'); rewrite(f2);
readln(f1,n,k);
for i:=1 to n do
read(f1,q[i]);
close(f1);
head:=1;
tail:=k;
sp:=0;
max:=-maxlongint;
min:=maxlongint;
for i:=1 to k do begin
if q[i]>max then max:=q[i];
if q[i]<min then min:=q[i];
end;
inc(sp);
h1[sp]:=min;
h2[sp]:=max;
end;
procedure play;
var i:longint;
begin
inc(tail);
inc(head);
while tail<=n do begin
if q[head-1]=max then begin
max:=-maxlongint;
for i:=head to tail-1 do
if q[i]>max then max:=q[i];
end;
if q[head-1]=min then begin
min:=maxlongint;
for i:=head to tail-1 do
if q[i]<min then min:=q[i];
end;
if q[tail]<min then min:=q[tail];
if q[tail]>max then max:=q[tail];
inc(sp);
h1[sp]:=min;
h2[sp]:=max;
inc(tail);
inc(head);
end;
end;
procedure print;
var i:longint;
begin
for i:=1 to sp do
write(f2,h1[i],' ');
writeln(f2);
for i:=1 to sp do
write(f2,h2[i],' ');
close(f2);
end;
begin
init;
play;
print;
end.
d.