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

int loop,a[100000];

void maxHeapify(int i){
  int l, r, largest,tmp;
  l = 2 * i;
  r = 2 * i + 1;
  if (l <=loop && a[l] > a[i]){
    largest = l;
  }
  else {
    largest = i;
  }
  if (r <= loop && a[r] > a[largest]){
    largest = r;
  }
  if (largest != i){
    tmp = a[i];
    a[i] = a[largest];
    a[largest] = tmp;
    maxHeapify(largest) ;// ?????°??????????????????
  }
}

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

int main(){
  scanf("%d",&loop);
  int i;
  for (i = 1;i<=loop; i++){
    scanf("%d",&a[i]);
  }
  buildMaxHeap();
  for (i = 1; i<= loop; i++){
    printf(" %d",a[i]);
  }
  printf("\n");
  return 0;
}