#include<stdio.h>
#define N 500002
 
typedef struct{
  int p;
  int l;
  int r;
  int k;
}Node;
 
Node node[N],ex;
int n,i;
 
void maxHeapify(int i){
  int l = 2*i, r = 2*i+1, largest;
  
  if(l <= n && node[l].k > node[i].k){
    largest = l;
  }else{
    largest = i;
  }
  if(r <= n && node[r].k > node[largest].k){
    largest = r;
  }
  if(largest != i){
    ex = node[i];
    node[i] = node[largest];
    node[largest] = ex;
    maxHeapify(largest);
  }
}
 
void buildMaxHeap(){
  for(i = n/2; i > 0; i--){
    maxHeapify(i);
  }
}
 
int main(){
  scanf("%d",&n);
  
  for(i = 1; i < n+1; i++){
    scanf("%d",&node[i].k);
  }
  
  for(i = 1; i < n+1; i++){
    if(i == 1){
      node[i].p = -1;
    }else{
      node[i].p = i/2;
    }
    if(2*i > n){
      node[i].l = node[i].r = -1;
    }else{
      node[i].l = 2*1;
    }
    if(2*i+1 > n){
      node[i].r = -1;
    }else{
      node[i].r = 2*i+1;
    }
  }
  
  buildMaxHeap();
  
  for(i = 1; i < n+1; i++){
    printf(" %d", node[i].k);
  } 
  printf("\n");
  return 0;
}