#include<stdio.h>

#define MAX 500000

int Tree[MAX];

int n;

void maxHeapify(int i){
  int largest ,exchange;
  unsigned int l, r;
  l = getLeft(i);
  r = getRight(i);
  
  if( ( n >= l ) && ( Tree[l] > Tree[i] ) ){
    largest = l;
  }else{
    largest = i;  
  }
  if( ( n >= r ) && ( Tree[r] > Tree[largest] ) ){
    largest = r;
  }
  if (largest != i){
    exchange =  Tree[i];
    Tree[i] = Tree[largest];
    Tree[largest] = exchange;
    maxHeapify(largest);
  }  
}

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

int getParent( int i ){
  return i/2;
}

int getLeft(int i){
  return (2*i);
}

int getRight(int i){
  return (2*i + 1);
}

main(){
  int i;
  scanf("%d", &n );

  for(i = 1; i <= n; i++){
    scanf("%d", &Tree[i]);
  }

  buildMaxHeap();

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

  return 0;
}