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

void maxH(int *, int, int);

main(){
  int i, n;
  int t[500001];

  scanf("%d", &n);

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

  for(i = n/2;i > 0;i--) maxH(t, i, n);


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

  printf("\n");
  return 0; 
}
void maxH(int *t, int i, int n){
  int l, r, large, c;

  if( i*2+1 < n + 1) r = i*2+1;
  else r = i;
  if( i*2 < n + 1) l = i*2;
  else l = i;

  if(l <= n && t[l] > t[i]) large = l;
  else large = i;

  if(r <= n && t[r] > t[large])large = r;

  if(large != i){
    c = t[i];
    t[i] = t[large];
    t[large] = c;
    maxH(t, large, n);
  }
}