#include <stdio.h>
#define MAX 500000

/* Prototype Declaration */
void maxHeapify(int);
void buildMaxHeap();

/* Global Variables */
int H;
int A[MAX + 1];

int main() {
  int i;

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

  //Build
  buildMaxHeap();

  for ( i = 1; i <= H; i++ )
    printf(" %d", A[i]);

  printf("\n");

  return 0;
}

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

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

  if ( left <= H && A[left] > A[i] )
    largest = left;
  else
    largest = i;

  if ( right <= H && A[right] > A[largest] )
    largest = right;

  if ( largest != i ) {
    tmp = A[i];
    A[i] = A[largest];
    A[largest] = tmp;
    maxHeapify(largest);
  }

}