#include <stdio.h>
#define N 500001
#define NIL 200000
void maxHeapify(int);
void buildMaxHeap();
int heap[N],n;

int main(){
  int i;
  for( i = 0 ; i < N ; i++ ){
    heap[i] = NIL;
  }

  scanf("%d",&n);

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

  buildMaxHeap();

  for( i = 1 ; i <= n ; i++ ){
    printf(" %d",heap[i]);
  }   
  printf("\n");
  return 0;
}
  
  
void maxHeapify(int i){
  int l,r,largest,a;
    l = 2*i;
   
    r = 2*i+1;
    
  if( l <= n && heap[l] > heap[i] ) largest = l;
  else largest = i;
    
  if( r <= n && heap[r] > heap[largest] )  largest = r;
    
  if( largest != i){
    a = heap[largest];
    heap[largest] = heap[i];
    heap[i] = a;
    maxHeapify(largest);
  }
}
void buildMaxHeap(){
  int i;
  for( i = n/2 ; i >= 1 ; i--){
    maxHeapify(i); 
  }
}