#include<stdio.h>

void maxHeap(int *,int);

int N;

int main(){
  
  int i,A[500000];
  
    scanf("%d",&N);
    for(i=1; i<=N ; i++){
    scanf("%d",&A[i]);
  }
  
  for(i=N/2 ; i>=1 ; i--){
    maxHeap(&A[0],i);
  }
  
  for(i=1 ; i<=N ; i++){
    printf(" %d",A[i]);
  }
  
  printf("\n");
  
  return 0;
}

void maxHeap(int *A,int i){
   
   int temp,l,j,r,largest;
   
   l = i*2;
   r = i*2+1;
   
   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;
   
   maxHeap(&A[0],largest);	 
   }
 }

