#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;  //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操作后,数组形成堆
        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;
}