#include<stdio.h>
#define M 2000000

int si,d[M+1];

void mH(int i){
  int l,r,largest,t;
  l = 2 *i;
  r = 2*i+1;
  if(l <= si && d[l] > d[i]){
    largest = l;
  }
  else largest = i;

  if(r <= si &&d[r] > d[largest]){
    largest= r;
  }
  if(largest != i){
    t = d[i];
    d[i] = d[largest];
    d[largest] = t;
    mH(largest);
  }
}

int main(){
  int i;
  scanf("%d",&si);


  for(i=1;i<=si;i++){
    scanf("%d",&d[i]);
  }

  for(i=si/2;i>=1;i--){
    mH(i);
  }

  for(i=1;i<=si;i++){
    printf(" %d",d[i]);
  }
  printf("\n");

  return 0;
}
    

