#include<stdio.h>
  
#define MAX 500000
  
int Tree[MAX];  
int n;
  
void maxHeapify(int i){
  int largest ,exchange, l, r;
  l = getLeft(i);
  r = getRight(i);
    
  if((n+1 >l) && (Tree[l] > Tree[i])) largest = l;
  
  else largest = i;
    
  if((n+1 > r) && (Tree[r] > Tree[largest])) largest = r;
  
  if (largest != i){
  
    exchange = Tree[i];
    Tree[i] = Tree[largest];
    Tree[largest] = exchange;
    maxHeapify(largest);
  
  } 
}
  
void buildMaxHeap(){
  int i;
  for (i=(n/2); i+1>1; i--){
    maxHeapify(i);
     }
}
   
int getLeft(int i){
  return (2*i);
}
  
int getRight(int i){
  return (2*i + 1);
}
int getParent(int i){
  return i/2;
}
 
  
main()
{
  int i;
  scanf("%d",&n);
  
  for(i=1; i<=n; i++){
  
    scanf("%d",&Tree[i]);
  
  }
  
  buildMaxHeap();
  
  for(i=1; i<=n; i++){
      
    printf(" %d",Tree[i]);
  
  }
  
  printf("\n");
  
  return 0;
}
