#include<stdio.h>

#define Max 500001

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

void maxHeapify(Node *, int, int);
void buildMaxHeap(Node *, int);

int main(){
  int i, H;
  Node T[Max];

  scanf("%d",&H);
  for(i = 1; i <= H; i++){
    T[i].parent = NULL;
    T[i].left = NULL;
    T[i].right = NULL;
  }
  for(i = 1; i <= H; i++){
    scanf("%d",&T[i].key);
    if(i > 1) T[i].parent = T[i / 2].key;
  }
  for(i = 1; i <= H; i++){
    T[i].left = T[2 * i].key;
    if(2 * i > H) T[i].left = NULL;
    T[i].right = T[2 * i + 1].key;
    if(2 * i + 1 > H) T[i].right = NULL;
  }

  buildMaxHeap(T, H);
  
  for(i = 1; i < H; i++){
    printf(" %d", T[i].key);
  }
  printf(" %d\n", T[i].key);

  return 0;
}


void maxHeapify(Node *A, int i, int H){
  int l, r, largest, n;

  l = i * 2;
  r = i * 2 + 1;

  if((l <= H) && (A[l].key > A[i].key)) largest = l;
  else largest = i;

  if((r <= H) && (A[r].key > A[largest].key)) largest = r;

  if(largest != i){
    n = A[i].key;
    A[i].key = A[largest].key;
    A[largest].key = n;
    maxHeapify(A, largest, H);
  }

}

void buildMaxHeap(Node *A, int H){
  int i;

  for(i = H / 2; i >= 1; i--){
    maxHeapify(A, i, H);
  }
}