#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> >res,res1;
vector<pair<int, int> >vuk;
void resi(int poc, int kr)
{
    z=0;
    int ii;

for(ii=0;ii<res.size();ii++)
    if(res[ii].second>=poc && res[ii].second<=kr)
res1.push_back(make_pair(res[ii].first,res[ii].second));

res.clear();
for(ii=0;ii<res1.size();ii++)
    res.push_back(make_pair(res1[ii].first,res1[ii].second));
res1.clear();
for(ii=0;ii<res.size();ii++)
{
    if(res[ii].second-odg.size()<=k && res[ii].second>=tren)
    {
        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);
}

main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
cin>>n>>k;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
    res.push_back(make_pair(a[i],i));
sort(res.begin(),res.end());

resi(0,n-1);
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;
}