#include <stdio.h>
 
int heap;

int Right(int i){
  i=i*2+1;
  
  if(i>heap) return 500002;
  else return i;
  
}

int Left(int i){
  i=i*2;
  
  if(i>heap) return 500002;
  else return i;
  
}
 
void maxHeap(int A[],int i){
  int l = Left(i),r = Right(i);
  int largest,j;
 
  if( l <= heap && A[l] > A[i]) largest = l;
  
  else largest = i;
  
  if( r <= h && A[r] > A[largest]) largest = r;
  
  if (largest != i){
    j=A[i];
    A[i]=A[largest];
    A[largest]=j;
    maxHeap(A,largest);
  }
}
 
void buildMHeap(int A[]){
    int i;
    
    for(i=heap/2;i>=1;i--){
      maxHeap(A,i);
    }
    
}
 
int main(){
    int i,A[500001];
     
    scanf("%d",&heap);
     
    for(i=1;i<=heap;i++){
      scanf("%d",&A[i]);
    }
 
    buildMHeap(A);
 
    for(i=1;i<=heap;i++){
      printf(" %d",A[i]);
    }
    
    printf("\n");
    return 0;
 }