#include<stdio.h>
#define MAX 500001

 int A[MAX];
 int H;

 void maxHeap(int i){
           int left,right,largest,tmp;
           left=i*2;
           right=i*2+1;

           if(H>=left && A[i]<A[left])largest=left;
           else largest=i;
           if(H>=right && A[right]>A[largest])largest=right;

           if(largest!=i){tmp=A[largest];A[largest]=A[i];A[i]=tmp;maxHeap(largest);}
           
           }


 int main(){
        int i;
        scanf("%d",&H);
        for(i=1;i<=H;i++){scanf("%d",&A[i]);}
        
        for(i=H/2;i>=1;i--){maxHeap(i);}

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

