#include<stdio.h>

#define H 2000000

int size;
int A[H];

int left(i){
  return i*2;
}

int right(i){
  return i*2+1;
}

void maxHeapify(int i){
  int l = left(i);
  int r = right(i);
  int tmp,largest;
  // ????????????????????????????????§???????????§????????????????????¶
  if(l<=size && A[l]>A[i]) largest = l;
  else largest = i;
  if(r<=size && A[r]>A[largest]) largest = r;

  if(largest != i){ // i ???????????????????????§????????´???
      tmp = A[i];
    A[i] = A[largest];
    A[largest] = tmp;
    maxHeapify(largest); // ?????°??????????????????
      }

}
void  buildMaxHeap(){
  int i;
  for(i=size/2;i>=1;i--){
    maxHeapify(i);
      }
}


int main(void){
  int i;
  scanf("%d",&size);
  
  for(i=1;i<=size;i++) scanf("%d",&A[i]);

  buildMaxHeap();

  for(i=1;i<=size;i++){
    printf(" %d",A[i]);
  }
  printf("\n");

  return 0;
}