#include <stdio.h>
#define MAX 500000

int H,X[MAX];

void MHeap(int i){
    int l,r,lrge,temp;
    
    l=2*i;
    r=2*i+1;
    
    if(l <= H && X[l] > X[i])
        lrge=l;
        
    else
        lrge=i;
        
    if(r <= H && X[r] > X[lrge])
        lrge=r;

    if(lrge != i){
        temp=X[i];
        X[i]=X[lrge];
        X[lrge]=temp;
        MHeap(lrge);
    }
}

int main(void){
    int i;
    
    
    scanf("%d",&H);
    
    for(i=1;i<=H;i++)
        scanf("%d",&X[i]);
        
    for(i=H/2;i>=1;i--)
        MHeap(i);
        
    for(i=1;i<=H;i++)
        printf(" %d",X[i]);
        
    puts("");
    return 0;
}

