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


int parent(int p) {
  if( p != 1 ) {
    return (int)p/2;
  }else {
    return -1;
  }
}


int left(int p, int n) {
  if( p*2 <= n ) {
    return p*2;
  }else {
    return -1;
  }
}


int right(int p, int n) {
  if( p*2+1 <= n ) {
    return p*2+1;
  }else {
    return -1;
  }
}


void swap(int* a, int* b) {
  int tmp;

  tmp = *a;
  *a = *b;
  *b = tmp;

}


void maxHeapify(int* h, int p, int n) {

  int l, r;
  int largest;

  l = left(p, n);
  r = right(p, n);

  //左、右、自身のうち最大のノード
  if ( h[p] >= h[l] && h[p] >= h[r] ) {
    largest = p;
  }
  else if( h[l] >= h[p] && h[l] >= h[r] ) {
    largest = l;
  }
  else {
    largest = r;
  }

  if( largest != p ) {
    swap(&h[p], &h[largest]);
    maxHeapify(h, largest, n);
  }

}


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

}


void printHeap(int* h, int n) {
  int i;

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

}


int main() {

  int n; //heapSize
  int* h; //heapPointer

  int i;

  scanf("%d", &n);

  h = (int*)malloc( sizeof(int) * (n + 1) );

  //Heapは1から始まる
  for( i=1; i<=n; i++) {
    scanf("%d", &h[i]);
  }

  buildMaxHeap(h, n);

  printHeap(h, n);

  return 0;

}