#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[1000010],q[1000010];
int n,k;
void minqueue()
{
int h = 0, t = -1;
for (int i = 0; i < n; ++i)
{
while (h <= t && q[h] + k <= i)
h++;
while (h <= t && a[i] < a[q[t]])
t--;
q[++t] = i;
if (i >= k - 1)
{
cout<<a[q[h]];
if (i != n - 1)cout<<" ";
}
}
cout<<endl;
}
void maxqueue()
{
int h = 0, t = -1;
for (int i = 0; i < n; ++i)
{
while (h <= t && q[h] + k <= i)
h++;
while (h <= t && a[i] > a[q[t]])
t--;
q[++t] = i;
if (i >= k - 1)
{
cout<<a[q[h]];
if (i != n - 1)cout<<" ";
}
}
cout<<endl;
}
signed main()
{
cin>>n>>k;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
minqueue();
maxqueue();
}