#include <iostream>
#include <iomanip>
using namespace std;

void swap(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

void maxHeapify(int heap[], int size, int i) {
    int smallest = i;
    int left = 2 * i + 1;
    int right = 2 * i + 2;

    if (left < size && heap[left] > heap[smallest])
        smallest = left;

    if (right < size && heap[right] > heap[smallest])
        smallest = right;

    if (smallest != i) {
        swap(heap[i], heap[smallest]);
        maxHeapify(heap, size, smallest);
    }
}

void insertElement(int heap[], int& size, int value) {
    // Add element at the end
    heap[size] = value;
    int i = size;
    size++;

    // Fix min heap property
    while (i > 0 && heap[(i - 1) / 2] > heap[i]) {
        swap(heap[i], heap[(i - 1) / 2]);
        i = (i - 1) / 2;
    }
}

void buildMaxHeap(int heap[], int size) {
    for (int i = size / 2 - 1; i >= 0; i--)
        minHeapify(heap, size, i);
}

int main() {
    int n;
    cin >> n;
    int heap[n];
    
    int size = 0;
    
    for (int i = 0; i < n; i++) {
        int value;
        cin >> value;
        insertElement(heap, size, value);
    }
    
    buildMaxHeap(heap, size);

   
    for (int i = 0; i < size; i++) {
        cout << heap[i] << " ";
    }
    return 0;
}