#include<stdio.h>

#define N 2000000

int Htw,Atw[N+1];


void swap(int *a, int*b){
    int tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
}

void maxHeapify(int i){
    int l,r,largest;
    l=2*i;
    r=2*i+1;


    if(l<=Htw && Atw[l]>Atw[i])largest = l;
    else largest = i;
    if(r<=Htw && Atw[r] > Atw[largest])largest = r;

    if(largest != i){
        swap(&Atw[i],&Atw[largest]);
        maxHeapify(largest);
    }
}


int main(){
    int i;
    scanf("%d",&Htw);
    for(i=1;i<=Htw;i++)scanf("%d",&Atw[i]);

    for(i=Htw/2;i>=1;i--)maxHeapify(i);

    for(i=1;i<=Htw;i++)printf(" %d",Atw[i]);


    printf("\n");

    return 0;
}
