#include <stdio.h>
#define MAX 2000000

int size,a[MAX+1];

void maxHeapify(int i){
  int left,right,largest,b;
 
  left = 2 * i ;
  right = 2 * i + 1;

  if( left<=size && a[left] >a[i] )largest=left;

  else largest = i;

  if( right <=size && a[right] > a[largest] ) largest = right;

  if( largest != i ){
    b = a[i];
    a[i] = a[largest];
    a[largest] = b;
    maxHeapify(largest);
  }
}
int main(){

  int i;

  scanf("%d",&size);

  for( i=1 ; i<=size ; i++ ) scanf("%d",&a[i]);

  for( i=size/2 ; i>=1 ; i-- ) maxHeapify(i);

  for( i=1 ; i<=size ; i++ ){
    printf(" %d",a[i]);
  }
  
  printf("\n");

  return 0;

}
