#include<stdio.h>
#define N 500001

typedef struct{
  int parent;
  int left;
  int right;
  int key;
}Node;

Node node[N], swap;
int n;

void maxHeapify(int i){
  int l = left(i), r = right(i), largest;
  if(l <= n && node[l].key > node[i].key) largest = l;
  else largest = i;
  if(r <= n && node[r].key > node[largest].key) largest = r;
  if(largest != i){
    swap = node[i];
    node[i] = node[largest];
    node[largest] = swap;
    maxHeapify(largest);
  }
}

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

int parent(int i){
  return i/2;
}
int left(int i){
  return 2*i;
}
int right(int i){
  return 2*i+1;
}

int main(){
  int i, j;
  scanf("%d", &n);
  for(i = 1;i < n+1;i++){
    scanf("%d", &node[i].key);
    if(i == 1) node[i].parent = -1;
    else node[i].parent = parent(i);
    if(2*i > n) node[i].left = node[i].right = -1;
    else{
      node[i].left = left(i);
      if(2*i+1 > n) node[i].right = -1;
      else node[i].right = right(i);
    }
  }
  buildMaxHeap();
  for(i = 1;i < n+1;i++){
    printf(" %d", node[i].key);
  } 
  printf("\n");
  return 0;
}