#include <stdio.h>
int H;
int A[500000];

int L(int i){
        return 2*i;
}
int R(int i){
        return 2*i+1;
}
void maxHeapify(int i){
        int Large,swap;
        int l=L(i);
        int r=R(i);
        if(l<=H&&A[l]>A[i]) {
                Large=l;
        }
        else{
                Large=i;
        }
        if(r<=H&&A[r]>A[Large]) {
                Large=r;
        }
        if(Large!=i) {
                swap=A[i];
                A[i]=A[Large];
                A[Large]=swap;
                maxHeapify(Large);
        }
}
/*void buildmaxHeap(A[]){
   for(int i=H/2;i>1;i--){
    maxHeapify( A[],i)
   }*/
int main(){
        int i;
        scanf("%d",&H);
        for (i=1; i<=H; i++) {
                scanf("%d",&A[i]);
        }
        for(i=H/2; i>=1; i--){
          maxHeapify(i);
        }

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