#include <bits/extc++.h>
#define endl '\n'
typedef long long ll;
#define int ll
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
void Main() {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (auto &x: a) {
cin >> x;
}
deque<int> dq;
for (int i = 0; i < n; ++i) {
while (dq.size() && a[dq.back()] >= a[i]) {
dq.pop_back();
}
dq.push_back(i);
if (i - dq[0] == k) {
dq.pop_front();
}
if (i + 1 >= k) {
cout << a[dq[0]] << " ";
}
}
cout << endl;
dq.clear();
for (int i = 0; i < n; ++i) {
while (dq.size() && a[dq.back()] <= a[i]) {
dq.pop_back();
}
dq.push_back(i);
if (i - dq[0] == k) {
dq.pop_front();
}
if (i + 1 >= k) {
cout << a[dq[0]] << " ";
}
}
cout << endl;
}
// #define __CP_MULTI_TEST_CASES
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int t = 1;
#ifdef __CP_MULTI_TEST_CASES
cin >> t;
#endif
while (t--) {
Main();
}
return 0;
}