#include <stdio.h>
#include <stdlib.h>
 
void maxHeapify( int* , int , int );
void buildMaxHeap( int* , int );
 
int main(){
 
  int *a;
  int n,i,j;
 
  scanf("%d",&n);
  a = malloc(sizeof(int)*(n+1));
 
  for( i=1 ; i<n+1 ; i++ ){
    scanf("%d",&a[i]);
  }
 
  buildMaxHeap( a, n );
 
  for( i=1 ; i<n+1 ; i++ ){
    printf(" %d",a[i]);
  }
 
  printf("\n");
 
  free(a);
 
  return 0;
}
 
void maxHeapify( int *a , int i , int n ){
  int l,r,max,tmp;
 
  max = i;
  l = 2*i;
  r = 2*i+1;
 
  if( l <= n && a[l] > a[i] ) max = l;
 
  if( r <= n && a[r] > a[max] ) max = r;
   
 
  if( max != i ){
    tmp = a[i];
    a[i] = a[max];
    a[max] = tmp;
    return maxHeapify( a, max, n );
  }
 
}
 
 
void buildMaxHeap( int *a , int n ){
  int i;
 
  for( i=n/2 ; i>=1 ; i-- ){
    maxHeapify(a, i, n);
  }
}