#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#define PI acos(-1.0)
#define LINF 1000000000000000000LL
#define eps 1e-8
#define LL long long
#define MAXN 1000010
using namespace std;
const int INF=0x3f3f3f3f;
LL read()
{
LL x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
LL n,k;
LL a[MAXN],q1[MAXN],q2[MAXN],ans1[MAXN],ans2[MAXN];//q1:单调递增队列 q2:单调递减队列
int l1=1,l2=1,r1,r2;
int main()
{
n=read();
k=read();
//scanf("%lld%lld",&n,&k);
for(int i=1;i<=n;i++)
a[i]=read();
for(int i=1;i<=n;i++)
{
while(l1<=r1&&q1[l1]<=i-k) l1++;
while(l2<=r2&&q2[l2]<=i-k) l2++;
while(l1<=r1&&a[i]<a[q1[r1]]) r1--;
q1[++r1]=i;
while(l2<=r2&&a[i]>a[q2[r2]]) r2--;
q2[++r2]=i;
ans1[i]=a[q1[l1]];
ans2[i]=a[q2[l2]];
}
for(int i=k;i<=n;i++) printf("%lld ",ans1[i]);
puts("");
for(int i=k;i<=n;i++) printf("%lld ",ans2[i]);
puts("");
return 0;
}