#include<stdio.h>
#define N 500000
   
int H;
int A[N+1];
   
void maxHeap(int i){
  int max;
  int r=i*2+1,l=i*2;
  int temp=0;
  
  if(l <= H && A[l] > A[i]){
    max=l;
  }
  else {
    max=i;
  }
  if(r <= H && A[r] > A[max]){
    max=r;
  }  
  if(max != i){
    temp = A[i];
    A[i] = A[max];
    A[max] = temp;
    maxHeap(max);
  }
}
 
void buildMaxHeap(){
  int i=0;
  for(i = H/2; i >= 1; i--){
    maxHeap(i);
  }
}
   
int main(){
  int i,j;
  scanf("%d", &H);
  for(i = 1; i <= H; i++){
    scanf("%d", &A[i]);
  }
  B();
  for(i = 1; i <= H; i++){
    printf(" ");
    printf("%d", A[i]);
  }
  printf("\n");
   
  return 0;
}