#include<stdio.h>
#define MAX 2000000

int s, M[MAX+1];
void heap(int);
int main(){
  scanf("%d", &s);
  int i;
  for(i=1;i<=s;i++)scanf("%d", &M[i]);  
  for(i=s/2;i>=1;i--)heap(i);
  for(i=1;i<=s;i++){
    printf(" %d", M[i]);
  }
  printf("\n");
  return 0;
}
void heap(int i){
  int l, r, lt, t;
  l = 2*i;
  r = 2*i+1;
  if (l<=s&&M[l]>M[i])lt = l;
  else lt = i;
  if (r<=s&&M[r]>M[lt])lt = r;
  if (lt!=i){
    t=M[i];M[i]=M[lt];M[lt]=t;
    heap(lt);
  }
}

