#include<stdio.h>
#define Z 500001
 
int A[Z];
int n;
 
void MaxHeapify(int i){
  int Left = 2*i;
  int Right = 2*i+1;
  int Largest;
  int temp;
 
  if(Left <= n && A[Left] > A[i]){
    Largest = Left;
  }
  else{
    Largest = i;
  }
  if(Right <= n && A[Right] > A[Largest]){
    Largest = Right;
  }
  if(Largest != i){
    temp = A[i];
    A[i] = A[Largest];
    A[Largest] = temp;
    MaxHeapify(Largest);
  }
}
 
void BuildMaxHeap(void){
  int i;
  for(i = (n/2);i >=1;i--){
    MaxHeapify(i);
  }
}
 
int main(){
  
  int i;
 
  scanf("%d",&n);
  for(i = 1;i <= n;i++){
    scanf("%d",&A[i]);
  }
 
  BuildMaxHeap();
 
  for(i = 1;i <= n;i++){
    printf(" %d",A[i]);
  }
  printf("\n");
 
  return 0;
}