前面是神奇的快读
#include <iostream>
#include <vector>
#include <cstdio>
#include <deque>
#include <string>
#include <cstring>
using namespace std;
namespace io {
const int __SIZE = (1 << 21) + 1;
char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, __c, qu[55]; int __f, qr, _eof;
#define Gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
inline void flush () { fwrite (obuf, 1, oS - obuf, stdout), oS = obuf; }
inline void gc (char &x) { x = Gc(); }
inline void pc (char x) { *oS ++ = x; if (oS == oT) flush (); }
inline void pstr (const char *s) { int __len = strlen(s); for (__f = 0; __f < __len; ++__f) pc (s[__f]); }
inline void gstr (char *s) { for(__c = Gc(); __c < 32 || __c > 126 || __c == ' ';) __c = Gc();
for(; __c > 31 && __c < 127 && __c != ' '; ++s, __c = Gc()) *s = __c; *s = 0; }
template <class I> inline bool gi (I &x) { _eof = 0;
for (__f = 1, __c = Gc(); (__c < '0' || __c > '9') && !_eof; __c = Gc()) { if (__c == '-') __f = -1; _eof |= __c == EOF; }
for (x = 0; __c <= '9' && __c >= '0' && !_eof; __c = Gc()) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; }
template <class I> inline void print (I x) { if (!x) pc ('0'); if (x < 0) pc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10; while (qr) pc (qu[qr --]); }
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
} using io::pc; using io::gc; using io::pstr; using io::gstr; using io::gi; using io::print;
vector<int> nums;
vector<int> Max(vector<int> a,int k){
vector<int> res;
deque<int> q;
for(int i=0;i < a.size();i++){
if(!q.empty()&&q.front()<i-k+1) q.pop_front();
while(!q.empty()&&a[q.back()]<a[i]) q.pop_back();
q.push_back(i);
if(i-k+1>=0) res.push_back(a[q.front()]);
}
return res;
}
vector<int> Min(vector<int> a,int k){
vector<int> res;
deque<int> q;
for(int i=0;i < a.size();i++){
if(!q.empty()&&q.front()<i-k+1) q.pop_front();
while(!q.empty()&&a[q.back()]>a[i]) q.pop_back();
q.push_back(i);
if(i-k+1>=0) res.push_back(a[q.front()]);
}
return res;
}
int main(){
std::ios::sync_with_stdio(false);
int n,k;
cin>>n>>k;
int tp;
for(int i=1;i<=n;i++){
cin>>tp;
nums.push_back(tp);
}
vector<int> max=Max(nums,k);
vector<int> min=Min(nums,k);
for(int i=0;i<min.size();i++){
cout<<min[i]<<" ";
}
cout<<endl;
for(int i=0;i<max.size();i++){
cout<<max[i]<<" ";
}
return 0;
}