#include<bits/stdc++.h>
#define int long long
const int M = 1e6;
using namespace std;
deque<int> q,lgg;
int n,m;
int a[M];
int ma[M],mi[M];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1; i<=n; i++) {
cin>>a[i];
}
for(int i=1; i<=n; i++) {
while(!q.empty()&&q.front()<=i-m&&i>m) {
q.pop_front();
}
while(!q.empty()&&a[q.back()]<=a[i]) {
q.pop_back();
}
while(!lgg.empty()&&lgg.front()<=i-m&&i>m) {
lgg.pop_front();
}
while(!lgg.empty()&&a[lgg.back()]>=a[i]) {
lgg.pop_back();
}
q.push_back(i);
lgg.push_back(i);
if(i>=m) {
mi[i]=lgg.front();
ma[i]=q.front();
}
}
for(int i=m; i<=n; i++) {
cout<<a[mi[i]]<<" ";
}
cout<<"\n";
for(int i=m; i<=n; i++) {
cout<<a[ma[i]]<<" ";
}
return 0;
}
WA on #1#11 RE on #10