#include<stdio.h>

#define MAX 500001
int A[MAX];
int h;

int parent(int i){
  return i/2;
}
int left(int i){
  return 2*i;
}
int right(int i){
  return 2*i+1;
}
void maxHeapify(int i){
  int l,r,largest,temp;
  
  l = left(i);
  r = right(i);
  
  // choose max node among left child, me, and right child
  
  if(l <= h && A[l] > A[i])
    largest = l;
  else 
    largest = i;
  
  if(r <= h && A[r] > A[largest])
    largest = r;
  
  if(largest != i){ // A[i] > A[l],A[r]
    temp = A[i];
    A[i] = A[largest];
    A[largest] = temp;
    maxHeapify(largest);
  }
}
void buildMaxHeap(){
  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;
}