#include <stdio.h>
void maxheap(int* , int , int);
int main(){
  int num,i,array[500001];
  scanf("%d",&num);
  for(i=1;i<=num;i++) scanf("%d",&array[i]);
  for(i=num/2;i>=1;i--) maxheap(array,i,num);
  for(i=1;i<=num;i++) printf(" %d",array[i]);
  printf("\n");
  return 0;
}
void maxheap(int *array, int i, int num){
  int left,right,max,value;
  left=i*2;
  right=i*2+1;
  if(left<=num &&  array[left]>array[i]) max=left;
  else max=i;
  if(right<=num && array[right]>array[max]) max=right;
  if(max!=i){
    value=array[i];     /* exchange array[i] and array[max] */
    array[i]=array[max];
    array[max]=value;
    maxheap(array,max,num);
  }
}
  

