#include<stdio.h>
#define H 500000

void maxHeapify(int*,int);
void buildMaxHeap(int*);

int num;


int main(){
  int h[H],i;

  scanf("%d",&num);

  for(i=1; i<=num; i++) scanf("%d",&h[i]);

  buildMaxHeap(h);

  for(i=1; i<=num; i++) printf(" %d",h[i]);
  printf("\n");

  return 0;
}

void maxHeapify(int *h,int i){
  int left,right,largest,tmp;
  left = i*2;
  right = i*2+1;
  
  if(left<=num && h[left]>h[i]) largest = left;
  else largest = i;
  
  if(right<=num && h[right]>h[largest]) largest = right;
  
  if(largest != i){
    tmp = h[i];
    h[i] = h[largest];
    h[largest] = tmp;
    maxHeapify(h,largest); 
  }
  
}


void buildMaxHeap(int *h){
  int i;
  for(i=num/2; i>0; i--) maxHeapify(h,i);

}