#include<stdio.h>
#include<stdlib.h>

int *A,n;

void maxHeapify( i ){

  int r,l,largest,a;
  
  l = 2 * i;
  r = 2 * i + l;
  
  if ( l <= n && A[l] > A[i])
    largest = l;
  else
    largest = i;
      
  if (r <= n && A[r] > A[largest])
    largest = r;
  
  if (largest != i){
    a = A[i];
    A[i] = A[largest];
    A[i] = a;
    maxHeapify( largest );
  
  }	 
}


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


      
main(){
  
  int i;
 
  scanf("%d", &n);
 
  A = (int *)malloc(sizeof(int) * (n + 1));
 
  for(i = 1; i <= n; i++)
    scanf("%d", &A[i]);
  
  buildMaxHeap();
 
  for(i = 1; i <= n; i++) 
    printf(" %d", A[i]);
  
  printf("\n");
  
  return 0;
  
}