#include<stdio.h>
int heaps[500001],H;
int maxHeapify(int);
int main(void)
{
    int i;
    scanf("%d",&H);
    for(i=1;i<=H;i++) scanf("%d",&heaps[i]);
    for(i=H/2;i>=1;i--) maxHeapify(i);
    for(i=1;i<=H;i++) printf(" %d",heaps[i]);
    return 0;
}
int maxHeapify(int num)
{
    int max,tmp;
    if(heaps[num]<heaps[num*2]) max=num*2;
    else                        max=num;
    if(num*2+1<=H){
        if(heaps[max]<heaps[num*2+1]) max=num*2+1;
    }
    if(max!=num){
        tmp=heaps[num];
        heaps[num]=heaps[max];
        heaps[max]=tmp;
        maxHeapify(max);
    }
    return 0;
}