#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int n, k;
int a[1000005];
struct elem {
int x, i;
};
deque<elem> q;
int main() {
cin >> n >> k;
for (int i = 1 ; i <= n ; i++) cin >> a[i];
for (int i = 1 ; i <= n ; i++) {
while (!q.empty() && a[i] <= q.back().x) q.pop_back();
q.push_back({a[i], i});
while (i - k >= q.front().i) q.pop_front();
if (i >= k) cout << q.front().x << ' ';
}
cout << endl;
for (int i = 1 ; i <= n ; i++) {
while (!q.empty() && a[i] >= q.back().x) q.pop_back();
q.push_back({a[i], i});
while (i - k >= q.front().i) q.pop_front();
if (i >= k) cout << q.front().x << ' ';
}
q.clear();
return 0;
}
Subtask0最后一个点WA了