#include<stdio.h>
#define max 1000000+5
int a[max],q1[max]/*单调递增*/,q2[max]/*单调递减*/,ans1[max],ans2[max];
int n,k,h1,t1,h2,t2;
void q1_in(int i){ //入队
while(h1<=t1&&a[q1[t1]]>=a[i]){
t1--;
}
q1[++t1]=i;
}
void q2_in(int i){ //入队
while(h2<=t2&&a[q2[t2]]<=a[i]){
t2--;
}
q2[++t2]=i;
}
int main(){
while(~scanf("%d%d",&n,&k)){
int i;
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
h1=h2=1;t1=t2=0;
for(i=1;i<k;i++){
q1_in(i);
q2_in(i);
}
int c1=0,c2=0;
for(i=k;i<=n;i++){
q1_in(i);
q2_in(i);
while(q1[h1]<i-k+1){//出队
h1++;
}
while(q2[h2]<i-k+1){//出队
h2++;
}
ans1[c1++]=a[q1[h1]];
ans2[c2++]=a[q2[h2]];
}
for(i=0;i<c1;i++){
if(i){
printf(" ");
}
printf("%d",ans1[i]);
}
puts("");
for(i=0;i<c2;i++){
if(i){
printf(" ");
}
printf("%d",ans2[i]);
}
puts("");
}
}