#include<stdio.h>
#define MAX 2000001

int H;

int left(int i){
  return 2*i;
}
int right(int i){
  return 2*i+1;
}
void maxheap(int* ary,int i){
  int l,r,largest,tmp;
  l=left(i);
  r=right(i);
  if(l<=H && ary[l]>ary[i]){largest=l;}
  else{largest=i;}
  if(r<=H && ary[r]>ary[largest]){largest=r;}
  if(largest!=i){
    tmp=ary[i];
    ary[i]=ary[largest];
    ary[largest]=tmp;
    maxheap(ary,largest);
  }
}
void buildmaxheap(int* ary){
  int i;
  for(i=H/2;i>0;i--){
    maxheap(ary,i);
  }
}
int main(){
  int i,n,ary[MAX];
  scanf("%d",&H);
  for(i=1;i<=H;i++){
    scanf("%d",&ary[i]);
  }
  buildmaxheap(ary);
  for(i=1;i<=H;i++){
    printf(" %d",ary[i]);
  }
  printf("\n");
  return 0;
}

