#include <stdio.h>
#define MAX 500010
#define NIL -2000000001

int heap[MAX];

void maxHeapify(int, int);
void buildMaxHeap(int);

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

  //1 origin
  for(i = 0; i <= H; i++)
    heap[i] = NIL;

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

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

  return 0;
}

void maxHeapify(int i, int H){
  int l, r, largest, tmp;
  l = 2*i;
  r = 2*i+1;
  if(l <= H && heap[l] > heap[i])
    largest = l;
  else
    largest = i;
  if(r <= H && heap[r] > heap[largest])
    largest = r;

  if(largest != i){
    tmp = heap[i];
    heap[i] = heap[largest];
    heap[largest] = tmp;
    maxHeapify(largest, H);
  }
}

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