#include <stdio.h>
int n,hai[2000001];

void MaxH(int a){
  int x,y,L,change,left,right;
  left=2*a;
  right=2*a+1;

  if(n>=left && hai[left]>hai[a]){
    L=left;
  }
  else{L=a;}

  if(n>=right && hai[right]>hai[L]){
    L=right;
  }
  if(L!=a){
    change=hai[a];
    hai[a]=hai[L];
    hai[L]=change;
    MaxH(L);
  }
}

int main(){
  int i,j,k;

  scanf("%d",&n);

  for(i=1; i <=n; i++){
    scanf("%d",&hai[i]);
  }

  for(i=n/2; i>=1;i--){
    MaxH(i);
  }

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

