#include <iostream>
using namespace std;

#define MAX_SIZE 100

void swap(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
}

void heapify(int heap[], int n, int i) {
    int smallest = i;
    int left = 2 * i + 1;
    int right = 2 * i + 2;

    if (left < n && heap[left] < heap[smallest])
        smallest = left;
    if (right < n && heap[right] < heap[smallest])
        smallest = right;

    if (smallest != i) {
        swap(heap[i], heap[smallest]);
        heapify(heap, n, smallest);
    }
}

void buildMinHeap(int heap[], int n) {
    for (int i = n / 2 - 1; i >= 0; i--) {
        heapify(heap, n, i);
    }
    
     for (int i = n - 1; i > 0; i--) {
        swap(arr[0], arr[i]); 
        heapify(arr, i, 0);
    }
}

void display(int heap[], int n) {
    for (int i = 0; i < n; i++)
        cout << heap[i] << " ";
    cout << endl;
}



int main() {
    int heap[MAX_SIZE];
    int n;

    cin >> n;

    for (int i = 0; i < n; i++)
        cin >> heap[i];

    buildMinHeap(heap, n);

    cout << "Min Heap: ";
    display(heap, n);

    

    return 0;
}