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

int n, *a;
void maxheapify(int i){
  int l, r, m, c;
  l = 2*i;
  r = 2*i+1;
  if(l <= n && a[l] > a[i])
    m = l;
  else m = i;
  if(r <= n && a[r] > a[m])
    m = r;
  if(m != i){
    c = a[i];
    a[i] = a[m];
    a[m] = c;
    maxheapify(m);
  }
  return;
}
void buildmaxheap(){
  int i;
  for(i = n/2; i > 0; i--)
    maxheapify(i);
}

int main(){
  int i;
  scanf("%d",&n);
  a = malloc(sizeof(int)*(n+1));
  for(i = 1; i <= n; i++) scanf("%d",&a[i]);
  buildmaxheap();
  for(i = 1; i <= n; i++)
    printf(" %d",a[i]);
  printf("\n");
  free(a);
  return 0;
}