#include<stdio.h>
 
#define H 500001
 
 
int A[H];
int n;
 
main(){
  
  int i;
 
  scanf("%d",&n);
  for(i = 1;i <= n;i++){
    scanf("%d",&A[i]);
  }
 
  buildMaxHeap();
 
  for(i = 1;i <= n;i++){
    printf(" %d",A[i]);
  }
  printf("\n");
 
  return 0;
}

void maxHeapify(int i){
  int left = 2*i;
  int right = 2*i+1;
  int largest;
  int temp;
 
  if(left <= n && A[left] > A[i]){
    largest = left;
  }
  else{
    largest = i;
  }
  if(right <= n && A[right] > A[largest]){
    largest = right;
  }
  if(largest != i){
    temp = A[i];
    A[i] = A[largest];
    A[largest] = temp;
    maxHeapify(largest);
  }
}
 
void buildMaxHeap(void){
  int i;
  for(i = (n/2);i >=1;i--){
    maxHeapify(i);
  }
}