#include<stdio.h>
#define MAX 500000
int maxHeapify(int);
int H,A[MAX];
int main(){
  int i;
  scanf("%d",&H);
  for(i=1;i<=H;i++){
    scanf("%d",&A[i]);
  }
  for(i=H/2;i>=1;i--){
    maxHeapify(i);
  }
  for(i=1;i<=H;i++){
    printf(" %d",A[i]);
  }
  printf("\n");
  return 0;
}
int maxHeapify(int i){
  int l,r,q,biggest;
  l = i*2;
  r = i*2+1;
  if(l<=H && A[l]> A[i])  biggest= l;
  else  biggest = i;
  if(r<=H && A[r] > A[biggest]) biggest = r;
  if(biggest != i){
    q=A[i];
    A[i]=A[biggest];
    A[biggest]=q;
    maxHeapify(biggest);
  }
}
