#include<stdio.h>
int A[500001];
 int n;
 int maxh(int i){
       int l=i*2;
       int r=i*2+1;

       int max;
       if(l<=n&&A[l]>A[i]){max=l;}
       else max=i;
       if(r<=n&&A[r]>A[max]){max=r;}

       if(max!=i){
               int tmp=A[i];A[i]=A[max];A[max]=tmp;
               maxh(max);
               }
       }

 int main(){
       int i;
       
       scanf("%d",&n);
       for(i=1;i<=n;i++){
                 scanf("%d",&A[i]);
                 }
       for(i=n/2;i>=1;i--){
                 maxh(i);
                 }
       for(i=1;i<=n;i++){
                 printf(" %d",A[i]);
                 }
       printf("\n");
       return 0;
       }
