/* 单调队列,从最简单的滑动窗口开始 ,这个题是纯粹的裸单调队列题目,关键在于理解单调队列的思想 */点击打开链接
#include < iostream >
#include < cstdio >
#include < cstring >
#define N 1000005
using namespace std;
int a[N],nt,mt;
int n,m,out1[N],out2[N];
int in1[N],in2[N],r,l,p,q;
void A_queue(int i)
{
while(l<=r && a[in1[r]]<=a[i]) r--;
in1[++r]=i;//存储下标
}
void D_queue(int i)
{
while(p<=q && a[in2[q]]>=a[i]) q--;
in2[++q]=i;//存储下标
}
void c_in1(int i)
{
while(in1[l]<=i-m) l++;
out1[nt++] = a[in1[l]];
}
void c_in2(int i)
{
while(in2[p]<=i-m) p++;
out2[mt++] = a[in2[p]];
}
int main ()
{
freopen("dddl.txt","r",stdin);
int i;
while(scanf("%d%d",&n,&m)!=EOF)
{
r=q=-1;l=p=nt=mt=0;
for(i=0 ; i<n ; i++)
scanf("%d",&a[i]);
memset(in1,0,sizeof(in1));
memset(in2,0,sizeof(in2));
for(i=0 ; i<m ; i++)
A_queue(i),D_queue(i);
c_in1(i),c_in2(i);
for(i=m ; i<n ; i++)
{
A_queue(i);
D_queue(i);
c_in1(i);
c_in2(i);
}
for(i=0 ; i<nt ; i++)
printf(i!=nt-1?"%d ":"%d\n",out2[i]);
for(i=0 ; i<mt ; i++)
printf(i!=mt-1?"%d ":"%d\n",out1[i]);
}
return 0;
}