#include<stdio.h>
#define N 500001
int total;
int left(int i){
  i*=2;
  if(i>total)return N;
  else return i;
}
int right(int i){
  i=2*i+1;
  if(i>total)return N;
  else return i;
}
void maxheap(int A[],int i){
  int hidari,migi,max,comp;
  hidari=left(i);
  migi=right(i);
  if(hidari<=total&&A[hidari]>A[i])
    max=hidari;
  else max=i;
  if(migi<=total&&A[migi]>A[max])
    max=migi;
  if(max!=i){
    comp=A[i];
    A[i]=A[max];
    A[max]=comp;
    maxheap(A,max);
  }
}
void maxheap2(int A[]){
  int i;
  for(i=total/2;i>=1;--i){
    maxheap(A,i);
  }
}
int main(){
  int i,papa,l,r,key[N];
  scanf("%d",&total);
for(i=1;i<=total;++i){
  scanf("%d",&key[i]);
 }
maxheap2(key);
for(i=1;i<=total;++i){
  printf(" %d",key[i]);
 }
printf("\n");
return 0;
}
