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