#include<stdio.h>
#define MAX 500005

int n;

void swap(int *a, int *b) {
    int t;
    t = *a;
    *a = *b;
    *b = t;
}

void maxHeapify(int *A, int i) {
    int largest;
    int l = 2 * i;
    int r = 2 * i + 1;
    if (l <= n && A[l] > A[i]) largest = l;
    else largest = i;
    if (r <= n && A[r] > A[largest]) largest = r;

    if (largest != i) {
        swap(&A[i], &A[largest]);
        maxHeapify(A, largest);
    }

}

void buildMaxHeap(int *A){
    int i;
    for (i = n/2; i>0; i--)
        maxHeapify(A, i);
} 

int main() {
    int i, j;
    int A[MAX];

    scanf("%d", &n);
    for(i=1;i<=n;i++) scanf("%d", &A[i]);

    buildMaxHeap(A);

    for(i=1;i<=n;i++) printf(" %d", A[i]);

    return 0;
}