#include <stdio.h>
#include <math.h>

void maxHeapify(int a[],int i,int H){
    int max=i;
    int l,r;
    l=i*2;
    r=i*2+1;
    
    if(l<=H&&a[i-1]<a[l-1])max=l;
    
    if(r<=H&&a[max-1]<a[r-1])max=r;
    
    int tmp;
    if(max!=i){
        tmp=a[i-1];
        a[i-1]=a[max-1];
        a[max-1]=tmp;
        
        maxHeapify(a,max,H);
    }
    
    return;
}
void buildMaxHeap(int a[],int H){
    int i;
    for(i=H/2;i>=0;i--)maxHeapify(a,i+1,H);
    return;
}

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