#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int a[N], n, k;
deque <int> q;
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
while (!q.empty() && a[q.back()] > a[i]) q.pop_back();
q.push_back(i);
if (i >= k) {
while (!q.empty() && q.front() <= i - k) q.pop_front();
cout << a[q.front()] << ' ';
}
}
cout << '\n';
while (!q.empty()) q.pop_back();
for (int i = 1; i <= n; i++) {
while (!q.empty() && a[q.back()] < a[i]) q.pop_back();
q.push_back(i);
if (i >= k) {
while (!q.empty() && q.front() <= i - k) q.pop_front();
cout << a[q.front()] << ' ';
}
}
return 0;
}