#include<stdio.h>
#define N 500000

void maxHeapify(int *,int);
void buildMaxHeap(int *);

int left[N],right[N],H;

int main(){
  int A[N],i,j=0;
  
  scanf("%d",&H);
  for(i=1 ; i<H+1 ; i++){
    scanf("%d",&A[i]);
    if(i==1) continue;
    else if(i%2==0) left[i/2]=i;
    else right[i/2]=i;
  }
  
  buildMaxHeap(A);
  
  for(i=1 ; i<H+1 ; i++){
  printf(" %d",A[i]);
  }
  return 0;
}

void maxHeapify(int *A,int i){
  int largest,k,l,r;
  l=left[i];
  r=right[i];
  
  if(i<=H && A[l]>A[i]) largest = l;
  
  else largest = i;
  if(i<=H && A[largest]<A[r]) largest=r;  
  if(largest != i){
    k = A[i];
    A[i] = A[largest];
    A[largest] = k;
    maxHeapify(A,largest);
  }
}

void buildMaxHeap(int *A){
  int i;
  for(i=H/2 ; i>=1 ; i--){
    maxHeapify(A,i);
  }

}

