#include <stdio.h>
#define MAX 500001
 
int heap[MAX];
int size;
 
int left(int i){
  return 2 * i;
}
 
int right(int i){
  return 2 * i + 1;
}
 
int parent(int i){
  return i / 2;
}
 
void maxHeapify(int i){
  int l, r, n, largest;
 
  l = left(i);
  r = right(i);
 
  if( l < size && heap[l] > heap[i] ){
    largest = l;
  }
  else{
    largest = i;
  }
 
  if( r < size && heap[r] > heap[largest] ){
    largest = r;
  }
 
  if( largest != i ){
    n = heap[i];
    heap[i] = heap[largest];
    heap[largest] = n;
   
    maxHeapify(largest);
  }
}
 
void buildMaxHeap(void){
  int i;
 
  for( i = size /2; i > 0; i--){
    maxHeapify(i);
  } 
}
 
main(){
  int i;
 
  scanf("%d", &size);
 
  size = size + 1;
 
  for( i = 1; i < size; i++){
    scanf("%d", &heap[i]);
  }
 
  buildMaxHeap();
 
  for( i = 1; i < size; i++){    
    printf(" %d", heap[i]);
  }
  printf("\n");
  return 0; 
}