#include<stdio.h>
#define H 2000000

int P[H+1],n;
int parent(int);
int Left(int);
int Right(int);
void maxHeapify(int i){
  int left,right,large,temp;
  left = Left(i);
  right = Right(i);
  if(left <= n && P[left] > P[i]) large = left;
  else large = i;
  if(right <= n && P[right] > P[large])large = right;

  if(large != i){
    temp = P[i];
    P[i] = P[large];
    P[large] = temp;
    maxHeapify(large);
  }
}
    
int main(){
  int i,p;
  
  scanf("%d", &n);
  for(i = 1;i<=n;i++){
    scanf("%d" , &P[i]);
  }
  for(i = n/2;i>=1;i--){
    maxHeapify(i);
  }
  for(i = 1; i <= n;i++){
    printf(" %d", P[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;
}