#include<stdio.h>
#define N 500001
#define NIL -2000000001
typedef struct{
  int k, p, l, r;
} node;
node A[N];

int hs = 0;

void maxheap(int i){
  int le, ri, lar;
  node temp;
  le = 2*i;
  ri = 2*i+1;
  if(le <= hs && A[le].k > A[i].k) lar = le;
  else lar = i;
  if(ri <= hs && A[ri].k > A[lar].k) lar = ri;
  if(lar != i){
    temp = A[i];
    A[i] = A[lar];
    A[lar] = temp;
    maxheap(lar);
  }
}

int main(){
  int i;
  scanf("%d", &hs);
  for(i = 1; i <= N; i++){
    A[i].k = NIL;
    A[i].p = NIL;
    A[i].l = NIL;
    A[i].r = NIL;
  }
  for(i = 1; i <= hs; i++) scanf("%d", &A[i].k);
  for(i = 1; i <= hs; i++){
    if(i != 1) A[i].p = A[i/2].k;
    if(A[2*i].k != NIL) A[i].l = A[2*i].k;
    if(A[2*i+1].k != NIL) A[i].r = A[2*i+1].k;
  }
  for(i = hs/2; i > 0; i--){
    maxheap(i);
  }
  for(i = 1; i <= hs; i++){
    printf(" %d", A[i].k);
  }
  printf("\n");
return 0;
}