#include<stdio.h>
#define H 500001

int T[H];
int h;

void maxHeapify(int);

int main(){

  int i,j;

  scanf("%d",&h);


  for(i=1;i<=h;i++){
    scanf("%d",&T[i]);
  }
  
  for(j=h/2;j>=1;j--){
    maxHeapify(j);
  }
   

  for(i=1;i<=h;i++){
    printf(" %d",T[i]);
  }
  printf("\n");
  return 0;
}

void maxHeapify(int i){
  int l,r,largest,swap;

  if(h>=2*i)l=2*i; 
  else l=2000000001;  
    
  if(h>=2*i+1) r=2*i+1;
  else r=2000000001;

    if(l<=h && T[l]>T[i]) largest = l;

    else largest = i;

    if(r<=h && T[r]>T[largest]) largest = r;

    if(largest != i) {
       swap = T[i];
       T[i] = T[largest];
       T[largest] = swap;
       maxHeapify(largest);
    }
    else return;

}