#include<cstdio>
const int N=1000010;
int n,k,a[N],q[N],head,tail;
int main()
{
freopen("window.in","r",stdin);
freopen("window.out","w",stdout);
scanf("%d%d",&n,&k);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
head=1;tail=0;
for (int i=1;i<k;i++){
for (;head<=tail&&a[q[tail]]>a[i];tail--);
q[++tail]=i;
}
for (int i=k;i<=n;i++){
for (;head<=tail&&q[head]<=i-k;head++);
for (;head<=tail&&a[q[tail]]>a[i];tail--);
q[++tail]=i;
printf("%d ",a[q[head]]);
}
puts("");
head=1;tail=0;
for (int i=1;i<k;i++){
for (;head<=tail&&a[q[tail]]<a[i];tail--);
q[++tail]=i;
}
for (int i=k;i<=n;i++){
for (;head<=tail&&q[head]<=i-k;head++);
for (;head<=tail&&a[q[tail]]<a[i];tail--);
q[++tail]=i;
printf("%d ",a[q[head]]);
}
puts("");
return 0;
}