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

