#include<stdio.h>
#define MAX_SIZE 500001

int Left(int i){return i*2;}
int Right(int i){return i*2+1;}

void SwapElement(int *tree, int i, int j){
  int buffer;
  buffer = tree[i];
  tree[i] = tree[j];
  tree[j] = buffer;
}

void maxHeapify(int *tree, int i, int n){
  int left = Left(i);
  int right =  Right(i);
  int largest;
  
  if(left<=n && tree[left]>tree[i]){
    largest = left;
  }else{
    largest = i;
  }
  if(right<=n && tree[right]>tree[largest]){
    largest = right;
  }
  
  if(largest != i){
    SwapElement(tree,i,largest);
    maxHeapify(tree,largest,n);
  }
}

void BuildMaxHeap(int *tree, int n){
  int i;
  for(i=n/2; i>=1; i--){
        maxHeapify(tree,i,n);
  }
}

int main(){
  int tree[MAX_SIZE];
  int i,num;
  
  scanf("%d",&num);
  for(i=1; i<=num; i++){
    scanf("%d",&tree[i]);
  }
  
  BuildMaxHeap(tree,num);
  for(i=1; i<=num; i++){
    printf(" %d",tree[i]);
  }
  printf("\n");
  return 0;
}

