#include<stdio.h>
#define N 500000
 
int n;

int left(int i){
    return 2*i;
}
int right(int i){
    return 2*i+1;
}
 
void maxHeapify(int A[], int i){
    int lar;
    int le,ri;
    int tmp;
    le = left(i);
    ri = right(i);
     
    if(le<=n && A[le] > A[i]) lar=le;
    else lar=i;
     
    if(ri<=n && A[ri]>A[lar]) lar=ri;
     
    if(lar != i){
        tmp=A[i];
        A[i]=A[lar];
        A[lar]=tmp;
        maxHeapify(A,lar);
    }
     
}
 
void buildMaxHeap(int A[]){
    int i;
    for(i=n/2;i>=1;i--){
        maxHeapify(A, i);
    }
}
 
 
int main(void){
    int i;
 
    int heap[N];
    scanf("%d",&n);
    for(i=1;i<n+1;i++){
        scanf("%d",&heap[i]);
    }
     
    buildMaxHeap(heap);
 
     
    for(i=1;i<n+1;i++){
        printf(" %d",heap[i]);
    }
    printf("\n");
    return 0;
}