#include<stdio.h>
int maxHeapify(int);
int XXX[500010];
int T,M,i,l,r,k=0;
 
int main(void){
  scanf("%d",&T);
  for(i=1;i<=T;i++){
    scanf("%d",&XXX[i]);
  }
  for(i=T/2;i>0;i--){
    maxHeapify(i);
  }
  for(i=1;i<=T;i++){
    printf(" %d",XXX[i]);
  }
  printf("\n");
  return 0;
}
 
int maxHeapify(i){
  l=2*i;
  r=2*i+1;
  if(l<=T && XXX[l]>XXX[i])M=l;
  else M=i;
  if(r<=T && XXX[r]>XXX[M])M=r;
  if(M!=i){
    k=XXX[i];
    XXX[i]=XXX[M];
    XXX[M]=k;
    return maxHeapify(M);
  }
  return 0;
}