#include<stdio.h>
int H;
int A[500002];
void maxHeapify(int n){
  int l = n*2;
  int r = l+1;
  int large;
  int tmp;
  if(l<=H && A[l]>A[n]){
    large = l;
  }
  else large = n;
  if(r<=H && A[r]>A[large]){
    large = r;
  }

  if(large !=n){
    tmp = A[n];
    A[n] = A[large];
    A[large] = tmp;
    maxHeapify(large);
  }
}
void buildMaxHeap(void){
  int i;
  for(i=H/2;i>0;i--) maxHeapify(i);
}
int main(){
  int i;
  scanf("%d",&H);
  for(i=1;i<=H;i++){
    scanf("%d",&A[i]);
  }
  buildMaxHeap();
  for(i=1;i<=H;i++){
    printf(" %d",A[i]);
  }
  printf("\n");
  return 0;
}


