#include<stdio.h>
#define HeapSize 500000

void MaxHeapify(int * ,int ,int );
void BuildMaxHeap(int *,int );
int left(int );
int right(int );

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