#include <stdio.h>
#define NIL -1
 
int n;
 
int getleft(int i){
    i *= 2;
    if(i > n) return NIL;
    else return i;
}
 
int getright(int i){
    i = 2*i+1;
    if(i > n) return NIL;
    else return i;
}
 

void maxHeapify(int A[],int i){
  int l,r,largest,temp;
  
  l = getleft(i);
  r = getright(i);

  if(l <= n && A[l] > A[i])
    largest = l;
  else largest = i;
  

  if(r <= n && A[r] > A[largest])
    largest = r;
  
  if(largest != i){
    temp = A[i];
    A[i] = A[largest];
    A[largest] = temp;
    maxHeapify(A,largest);
  }
  
}

void buildMaxHeap(int A[]){
  int i;
  for(i=n/2; i>=1; i--){
    maxHeapify(A,i);
  }
  }


int main(){
  int i,parent,left,right,key[500000];
  
  scanf("%d",&n);
  for(i=1; i<=n; i++){
    scanf("%d",&key[i]);
  }
  
  buildMaxHeap(key);
  
  for(i=1; i<=n; i++){
    printf("%d",key[i]);
    if(i >= 1 && i < n)
      printf(" ");
  }
  
  printf("\n");
  
  return 0;
}