#include<stdio.h>
#define H 1000000
int A[H];
int left(int);
int right(int);
int parent(int);
int maxHeapify(int,int);
void buildMaxHeap(int);
int main(){
  int i,n;
  scanf("%d",&n);
  for(i = 1;i <= n;i++){
    scanf("%d",&A[i]);
  }
  buildMaxHeap(n);
  for(i = 1;i <= n;i++){
    if(i)printf(" ");
    printf("%d",A[i]);
  }
  printf("\n");
}
int left(int i){
  return 2 * i;
}
int right(int i){
  return 2 * i + 1;
}
int parent(int i){
  if(i%2 == 1)return A[i/2];
  else return A[i/2-1];
}
int maxHeapify(int i,int size){
  int swap;
  int largest,r,l;
  r = right(i);
  if(r > size){
    A[r] = -2000000001;
  }
  l = left(i);
  if(l <= size && A[l] > A[i]){
    largest = l;
  }
    else{
    largest = i;
  }
  if(r <= size && A[r] > A[largest]){
    largest = r;
  }
  if(largest != i){
    swap = A[i];
    A[i] = A[largest];
    A[largest] = swap;
    maxHeapify(largest,size);
  }
}
void buildMaxHeap(int size){
  int i;
    for(i = size/2;i > 0;i--){
    maxHeapify(i,size);
  }
}
