#include<stdio.h>
  int H;
void maxHeapify(long *A,long i){
  long B[10];
  long left,right,largest;
    left = 2*i;
    right = 2*i+1;
    
    if(left <= H && A[left] > A[i]){
      largest = left;
    }
    else{
      largest = i;
    }
    if(right <= H && A[right] > A[largest]){
      largest = right;
    }
    if(largest != i){
      B[0] = A[i];
      A[i] = A[largest];
      A[largest] = B[0];
      maxHeapify(A,largest);
  }
}
int main(){
  long A[500000];
  long i;
  scanf("%d",&H);

  for(i = 1;i <= H;i++){
    scanf("%ld",&A[i]);
  }
  for(i = H/2;i >= 1;i--){
    maxHeapify(A,i);
  }
  for(i = 1;i <= H;i++){
    printf(" %ld",A[i]);
  }
  printf("\n");
  return 0;
}

