#include <bits/stdc++.h>
using namespace std;
int n,k,q[1000005],a[2000005],head=1,tail=1;
int main(){
cin>>n>>k;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++){
while(head<=tail&&a[q[tail]]>a[i]){
tail--;
}
q[++tail]=i;
while(i-q[head]+1>k){
head++;
}
if(i>=k){
printf("%d ",a[q[head]]);
}
}
printf("\n");
memset(q,0,sizeof(q));
for(int i=1;i<=n;i++){
while(head<=tail&&a[q[tail]]<a[i]){
tail--;
}
q[++tail]=i;
while(i-q[head]+1>k){
head++;
}
if(i>=k){
printf("%d ",a[q[head]]);
}
}
}