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

int *A, H;

void maxHeapify(int *, int);


int main() {
  int id;

  scanf( "%d" , &H );
  A = malloc((H+1) * sizeof(int));
  for ( id=1; id<=H; id++ ) scanf( "%d" , &A[id] );

  for ( id=H/2; id>=1; id-- ) maxHeapify(A, id);

  for ( id=1; id<=H; id++ ) printf( " %d" , A[id] );
  printf( "\n" );
  
  return 0;
}


void maxHeapify(int *A, int i) {
  int temp, l, r, largest;
  
  l = i*2;
  r = i*2+1;

  // 左の子、自分、右の子で値が最大のノードを選ぶ
  if ( (l <= H) && (A[l] > A[i]) ) largest = l;
  else largest = i;
  if ( (r <= H) && (A[r] > A[largest]) ) largest = r;
  if ( largest != i ) { // i の子の方が値が大きい場合
    temp = A[i];
    A[i] =  A[largest];
    A[largest] = temp;
    maxHeapify(A, largest); // 再帰的に呼び出し
  }
}

