#include<iostream> using namespace std; const int N = 100010; int h[N], cnt; void down( int k ){ int t = k; if(k * 2 <= cnt && h[k * 2] < h[t]) t = k * 2; if(k * 2 + 1 <= cnt && h[k * 2 + 1] < h[t]) t = k * 2 + 1; if( k != t){ swap(h[t], h[k]); down(t); } } int main(){ int n, m; cin>>n>>m; cnt = n; for(int i = 1; i <= n; i ++) cin >> h[i]; for(int i = n / 2; i ; i --) down (i); while(m--){ cout<<h[1]<<" "; h[1] = h[cnt --]; down(1); } return 0; }