#include<bits/stdc++.h>
using namespace std;
int n,a[500100],k,i,j,x,z,broj,tren=-1;
vector<int>odg;
vector<pair<int, int> >vuk;
void resi(int poc, int kr, int k)
{
    z=0;
    int ii;
vector<pair<int,int> >res;
for(int ii=poc;ii<=kr;ii++)
        res.push_back(make_pair(a[ii],ii));
sort(res.begin(),res.end());
for(ii=0;ii<res.size();ii++)
{
    if(res[ii].second-odg.size()<=k && res[ii].second>=tren && odg.size()+broj!=n-k)
    {
        odg.push_back(res[ii].first);
        tren=res[ii].second;
    }

    else
        if(res[ii].second>=tren && kr+broj-res[ii].second+1<n-k)
    {
        z=1;
    broj+=kr-res[ii].second+1;
    vuk.push_back(make_pair(res[ii].second,kr));
    break;
    }
    if(odg.size()+broj==n-k)
        break;
}

if(odg.size()+broj<n-k && z==1)
resi(tren+1,res[ii].second-1,k);
}

main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
cin>>n>>k;
for(i=0;i<n;i++)
cin>>a[i];
resi(0,n-1,k);
for(i=0;i<odg.size();i++)
cout<<odg[i]<<" ";
sort(vuk.begin(),vuk.end());
for(i=0;i<vuk.size();i++)
{
    for(j=vuk[i].first;j<=vuk[i].second;j++)
    cout<<a[j]<<" ";
}
return 0;
}