#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int heap[N], siz;
int n, m;
void down(int i) {
int t = i;
if (i * 2 <= siz and heap[i * 2] < heap[t]) t = i * 2;
if (i * 2 + 1 <= siz and heap[i * 2 + 1] < heap[t]) t = i * 2 + 1;
if (t != i) {
swap(heap[i], heap[t]);
down(t);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("D:/VS CODE/C++/in.txt", "r", stdin);
freopen("D:/VS CODE/C++/out.txt", "w", stdout);
#endif
cin >> n >> m;
siz = n;
for (int i = 1; i <= n; ++i) {
cin >> heap[i];
}
for (int i = n / 2; i ; --i) {
down(i);
}
for (int i = 0; i < m; ++i) {
printf("%d ", heap[1]);
heap[1] = heap[siz];
--siz;
down(1);
}
fclose(stdin);
fclose(stdout);
return 0;
}