#include<stdio.h>

int heap[500000];
int size;

void max_heapify(int heap[], int i) {
    int l = 2 * i + 1;
    int r = 2 * i + 2;
    int largest;

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

    if (largest != i) {
        int tmp = heap[i];
        heap[i] = heap[largest];
        heap[largest] = tmp;
        max_heapify(heap, largest);
    }
}

void build_max_heap(int heap[]) {
    for (int i = size / 2; i >= 0; i--) {
        max_heapify(heap, i);
    }
}

int main(){
    scanf("%d", &size);
    for(int i=0; i<size; i++) {
        int tmp;
        scanf("%d", &tmp);
        heap[i] = tmp;
    }

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