#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 1e6 + 7;
int q[N];
int a[N];
int main() {
IO;
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> a[i];
int hh = 0, tt = -1;
for (int i = 1; i <= n; ++i) {
if (hh <= tt && q[hh] <= i - m) ++hh;
while (hh <= tt && a[q[tt]] >= a[i]) --tt;
q[++tt] = i;
if (i >= m) cout << a[q[hh]] << " ";
}
cout << '\n';
hh = 0, tt = -1;
for (int i = 1; i <= n; ++i) {
if (hh <= tt && q[hh] <= i - m) ++hh;
while (hh <= tt && a[q[tt]] <= a[i]) --tt;
q[++tt] = i;
if (i >= m) cout << a[q[hh]] << " ";
}
return 0;
}