#include <stdio.h>
#define heap_size 500001

int Parent(int);
int Left(int);
int Right(int);
void MaxHeapify(int);
void BuildMaxHeap();
int A[heap_size],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;
}

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 left,right,tmp;
  int largest;
  left = Left(i);
  right = Right(i);
  if(left <= n && A[left] > A[i]){
    largest = left;
  } else {
    largest = i;
  }
  if(right <= n && A[right] > A[largest]){
    largest = right;
  }
  if(largest !=i){
    tmp = A[i];
    A[i]=A[largest];
    A[largest]= tmp;
    MaxHeapify(largest);
  }
}

void BuildMaxHeap(){
  int i;
  for(i=n/2;i>0;i--){
    MaxHeapify(i);
  }
}