#include<iostream> #include<algorithm> #include<cstring> #define int long long
usingnamespacestd;
constint maxn = 1000006; int n, k, a[maxn];
structnode {int val, pos; } q[maxn];
signedmain(){ cin >> n >> k; for (int i = 1; i <= n; i ++) cin >> a[i]; int head = 0, tail = 0; // find min -> increase for (int i = 1; i <= n; i ++) { while (head < tail && a[i] <= q[tail - 1].val) tail --; q[tail ++] = (node) { a[i], i }; while (head < tail && i - q[head].pos + 1 > k) head ++; if (i >= k) cout << q[head].val << (i == n ? "\n" : " "); } // find max -> decrease head = 0, tail = 0; for (int i = 1; i <= n; i ++) { while (head < tail && a[i] >= q[tail - 1].val) tail --; q[tail ++] = (node) { a[i], i }; while (head < tail && i - q[head].pos + 1 > k) head ++; if (i >= k) cout << q[head].val << (i == n ? "\n" : " "); } return0; }