#include<stdio.h>
#define N 500000
 
int H;
int A[N+1];
 
void MaxHeapify(int i){
  int l,r,largest,temp;

  l=i*2;
  r=i*2+1;
  temp=0;
 
  if(l<=H && A[l]>A[i]){
    largest=l;
  }else{
    largest=i;
  }
 
  if(r<=H && A[r]>A[largest]){
    largest=r;
  }
 
  if(largest!=i){
    temp=A[i];
    A[i]=A[largest];
    A[largest]=temp;
    MaxHeapify(largest);
  }
}
 
void buildMaxHeap(){
  int i=0;
 
  for(i=H/2;i>=1;i--){
    MaxHeapify(i);
  }
}
 
int main(){
  int i,j;
 
  scanf("%d",&H);
  for(i=1;i<=H;i++){
    scanf("%d",&A[i]);
  }
 
  buildMaxHeap();
 
  for(i=1;i<=H;i++){
    printf(" ");
    printf("%d",A[i]);
  }
  printf("\n");
 
  return 0;
}