#pragma GCC optimize("O3,unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define ll long long const ll INF_ADD=1e18; #define pb push_back #define mp make_pair #define nline "\n" #define f first #define s second #define pll pair<ll,ll> #define all(x) x.begin(),x.end() #define vl vector<ll> #define vvl vector<vector<ll>> #define vvvl vector<vector<vector<ll>>> #ifndef ONLINE_JUDGE #define debug(x) cerr<<#x<<" "; _print(x); cerr<<nline; #else #define debug(x); #endif void _print(ll x){cerr<<x;} void _print(char x){cerr<<x;} void _print(string x){cerr<<x;} mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template<class T,class V> void _print(pair<T,V> p) {cerr<<"{"; _print(p.first);cerr<<","; _print(p.second);cerr<<"}";} template<class T>void _print(vector<T> v) {cerr<<" [ "; for (T i:v){_print(i);cerr<<" ";}cerr<<"]";} template<class T>void _print(set<T> v) {cerr<<" [ "; for (T i:v){_print(i); cerr<<" ";}cerr<<"]";} template<class T>void _print(multiset<T> v) {cerr<< " [ "; for (T i:v){_print(i);cerr<<" ";}cerr<<"]";} template<class T,class V>void _print(map<T, V> v) {cerr<<" [ "; for(auto i:v) {_print(i);cerr<<" ";} cerr<<"]";} typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset; typedef tree<pair<ll,ll>, null_type, less<pair<ll,ll>>, rb_tree_tag, tree_order_statistics_node_update> ordered_pset; //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- const ll MOD=998244353; const ll MAX=100100; class ST{ public: vector<ll> segs; ll size=0; ll ID=INF_ADD; ST(ll sz) { segs.assign(2*sz,ID); size=sz; } ll comb(ll a,ll b) { return min(a,b); } void upd(ll idx, ll val) { segs[idx+=size]=val; for(idx/=2;idx;idx/=2){ segs[idx]=comb(segs[2*idx],segs[2*idx+1]); } } ll query(ll l,ll r) { ll lans=ID,rans=ID; for(l+=size,r+=size+1;l<r;l/=2,r/=2) { if(l&1) { lans=comb(lans,segs[l++]); } if(r&1){ rans=comb(segs[--r],rans); } } return comb(lans,rans); } }; class ST_nax{ public: vector<ll> segs; ll size=0; ll ID=-INF_ADD; ST_nax(ll sz) { segs.assign(2*sz,ID); size=sz; } ll comb(ll a,ll b) { return max(a,b); } void upd(ll idx, ll val) { segs[idx+=size]=val; for(idx/=2;idx;idx/=2){ segs[idx]=comb(segs[2*idx],segs[2*idx+1]); } } ll query(ll l,ll r) { ll lans=ID,rans=ID; for(l+=size,r+=size+1;l<r;l/=2,r/=2) { if(l&1) { lans=comb(lans,segs[l++]); } if(r&1){ rans=comb(segs[--r],rans); } } return comb(lans,rans); } }; void solve(){ ll n,k; cin>>n>>k; vector<ll> a(n+5,0); ST check_min(n+5); ST_nax check_max(n+5); for(ll i=1;i<=n;i++){ cin>>a[i]; check_min.upd(i,a[i]); check_max.upd(i,a[i]); } vector<ll> order; for(ll i=1;i<=n-k+1;i++){ order.push_back(i); } vector<ll> bad_sum(n+5,0); for(ll i=1;i<=n;i++){ bad_sum[i]=bad_sum[i-1]+(a[i]<a[i-1]); } auto comp=[&](ll l,ll r){ ll compare_perm=0; ll lf=l,rg=r; if(lf>rg){ swap(lf,rg); } ll first_end=lf+k-1,second_end=rg+k-1; if(first_end>=rg){ ll use_l=rg,use_r=first_end; if(bad_sum[use_l-1]!=bad_sum[lf]){ compare_perm=-1; } else if(a[use_l-1]>check_min.query(use_l,use_r)){ compare_perm=-1; } else if(check_max.query(use_l,use_r)>check_min.query(use_r+1,second_end)){ compare_perm=1; } else if(bad_sum[use_r]!=bad_sum[second_end]){ compare_perm=1; } } else{ if(bad_sum[first_end]!=bad_sum[lf]){ compare_perm=-1; } else if(bad_sum[second_end]!=bad_sum[rg]){ compare_perm=1; } } if(lf!=l){ compare_perm*=-1; } if(compare_perm==-1){ return true; } if(compare_perm==1){ return false; } return l<=r; }; sort(all(order),[&](ll l,ll r){ return comp(l,r); }); vector<ll> ans; for(ll i=1;i<=n;i++){ ans.push_back(a[i]); } ll best=order.back(); sort(ans.begin()+best-1,ans.begin()+best-1+k); for(auto it:ans){ cout<<it<<" "; } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif ll test_cases=1; //cin>>test_cases;; while(test_cases--){ solve(); } cout<<fixed<<setprecision(10); cerr<<"Time:"<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n"; }