#include <stdio.h>
void Maxheapify(int);

int h,X[5000000];

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

  for(i=1;i<=h;i++){
    scanf("%d",&X[i]);
  }
  for(i = h/2 ; i >=1 ; i--)Maxheapify(i);

  for(i=1;i<=h;i++){
    printf(" %d",X[i]);
  }
  printf("\n");
  return 0;
}
void Maxheapify(int x){
  int ri,le,large;
  int tmp;
  le=2*x;
  ri=2*x+1;

  if(le<=h &&X[le]>X[x])large=le;
  else large =x;

  if( ri <= h && X[ri] > X[large] )large=ri;
  if(large!=x){
    tmp=X[x];
    X[x]=X[large];
    X[large]=tmp;

    Maxheapify(large);
  }
}

