#include <stdio.h>
#define INF 2000000001
int heap[500001];
int size;

void buildMaxHeap();
void maxHeapify(int);
int main (){
    int n,i,tmp;
    scanf("%d",&n);
    size=n;
    for(i=1;i<=n;i++){
        scanf("%d",&heap[i]);
    }
    buildMaxHeap();
    for(i=1;i<=n;i++){
        printf(" %d",heap[i]);
    }
    printf("\n");
    return 0;
}
void buildMaxHeap(){
    int i;
    for(i=size/2;i>0;i--){
        maxHeapify(i);
    }
}
void maxHeapify(int i){
    int l,r,largest,tmp;
    l=i*2;
    r=i*2+1;
    if(l<=size && heap[l]>heap[i]){
        largest=l;
    }
    else{
        largest=i;
    }
    if(r<=size && heap[r]>heap[largest]){
        largest=r;
    }
    if(largest!=i){
        tmp=heap[i];
        heap[i]=heap[largest];
        heap[largest]=tmp;
        maxHeapify(largest);
    }
}
