#include<stdio.h>
#define N 500000

int data[N+1], n;

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

void maxHeapify(i){
    int l = 2*i;
    int r = 2*i+1;
    int valM;
    if(l <= n && data[l] > data[i]) valM = l;
    else valM = i;
    if(r<=n && data[r] > data[valM]) valM = r;
    if(valM != i){
        swap(&data[i], &data[valM]);
        maxHeapify(valM);
    }
}

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

int main(){
    int i;
    scanf("%d", &n);
    for(i=1; i<=n; i++) scanf("%d", &data[i]);
    buildMaxHeap();
    for(i=1; i<=n; i++) printf(" %d", data[i]);
    printf("\n");
    return 0;
}
