#include<stdio.h>
int H;
int A[500001];
void maxHeapify(int i){//ヒープソート
  int l = i*2;
  int r = l+1;
  int largest;
  int tmp;
  if(l<=H && A[l]>A[i]){
    largest = l;
  }
  else largest = i;
  if(r<=H && A[r]>A[largest]){
    largest = r;
  }
 
  if(largest !=i){
    tmp = A[i];
    A[i] = A[largest];
    A[largest] = tmp;
    maxHeapify(largest);
  }
}
void buildMaxHeap(void){
  int i;
  for(i=H/2;i>0;i--) maxHeapify(i);
}
int del(int i){
  int tmp;
  tmp = A[i];
  A[i] = A[H];
  A[H] = tmp;
  return A[H];
}
int main(){
  int i;
  scanf("%d",&H);
  for(i=1;i<=H;i++){
    scanf("%d",&A[i]);
  }
  buildMaxHeap();
   
  for(i=1;i<=H;i++){
    printf(" %d",A[i]);
  }
  printf("\n");
     
  return 0;
}
