#include <bits/stdc++.h> using namespace std; const int maxn=1e6+10; int a[maxn],q[maxn],hh,tt=-1; int main() { ios::sync_with_stdio(false); int n,k; cin>>n>>k; for(int i=0;i<n;i++){ cin>>a[i]; if(i-k+1>q[hh])hh++; while(hh<=tt&&a[i]<=a[q[tt]])tt--; q[++tt]=i; if(i+1>=k)cout<<a[q[hh]]<<" "; } cout<<"\n"; hh=0,tt=-1; for(int i=0;i<n;i++){ if(i-k+1>q[hh])hh++; while(hh<=tt&&a[i]>=a[q[tt]])tt--; q[++tt]=i; if(i+1>=k)cout<<a[q[hh]]<<" "; } return 0; }