#include <stdio.h>
#include <stdlib.h>

int H;

void maxHeapify(int *A,int i){
    int l=2*i,r=l+1,max;
    if(l<=H && A[l]>A[i]) max=l;
    else max=i;
    if(r<=H && A[r]>A[max]) max=r;

    if(max!=i){
        int t=A[i];
        A[i]=A[max];
        A[max]=t;
        maxHeapify(A,max);
    }
}

void buildMaxHeap(int *A){
    int i;
    for(i = H/2;i>0;i--)
        maxHeapify(A, i);
}

int main(void){
    scanf("%d",&H);
    int i,*A=malloc(sizeof(int)*(H+1));
    for(i=1;i<=H;i++)
        scanf("%d",A+i);
    buildMaxHeap(A);

    for(i=1;i<=H;i++)
        printf(" %d",A[i]);
    printf("\n");
    
    return 0;
}
