#include <stdio.h>

#define swap(typ, x, y) \
    do { \
        typ tmp; \
        tmp = x; \
        x = y; \
        y = tmp; \
    } while (0)

void max_heapify(int *arr, int i, int size) {
    int l, r, largest;

    l = (i+1) * 2 - 1;
    r = (i+1) * 2;

    if (l <= (size - 1) && arr[l] > arr[i]) {
        largest = l;
    } else {
        largest = i;
    }
    if (r <= (size - 1) && arr[r] > arr[largest]) {
        largest = r;
    }

    if (largest != i) {
        swap(int, arr[i], arr[largest]);
        max_heapify(arr, largest, size);
    }
}

void build_max_heap(int *arr, int size) {
    for (int i = (size / 2); i >= 0; i--) {
        max_heapify(arr, i, size);
    }
}

int main(void) {
    int h;
    int arr[500000];

    scanf("%d", &h);

    for (int i = 0; i < h; i++) {
        int x;
        scanf("%d", &x);
        arr[i] = x;
    }

    build_max_heap(arr, h);

    for (int i = 0; i < h; i++) {
        printf(" %d", arr[i]);
    }
    printf("\n");

    return 0;
}

