#include<stdio.h>
typedef struct{
  int id;
  int key;
  int leftKey;
  int rightKey;
  int parentKey;
}heap;

void insert(int, int);
void print(int);
void maxHeapify(int, int);
void buildMaxHeap(int);
heap Heap[500000];
int A[500000];

main(){
  int H, i;
  scanf("%d ", &H);
  for(i = 1; i <= H; i++){
    scanf(" %d", &A[i]);
  }
  for(i = 1; i <= H; i++){
    insert(i, H);
  }
  buildMaxHeap(H);
  for(i = 1; i <= H; i++){
    print(i);
  }
  printf("\n");
  return 0;
}

void insert(int id, int H){
  Heap[id].id = id;
  Heap[id].key = A[id];
  if(id/2 > 0)    Heap[id].parentKey = A[id/2];
  if(id*2 <= H)   Heap[id].leftKey = A[id*2];
  if(id*2+1 <= H) Heap[id].rightKey = A[id*2+1]; 
}

void maxHeapify(int H, int i){
  int x, l, r, largest, a;
  for(x = 1; x <= H; x++){
    if(Heap[i].leftKey == Heap[x].key)  l = Heap[x].id;
    if(Heap[i].rightKey == Heap[x].key) r = Heap[x].id;
  }
  if(l<=H && Heap[l].key > Heap[i].key)       largest = l;
  else if(l<=H && Heap[i].key > Heap[l].key)  largest = i;
  if(r<=H && Heap[r].key > Heap[largest].key) largest = r;
  if(largest != i){
    a = Heap[i].key;
    Heap[i].key = Heap[largest].key;
    Heap[largest].key = a;
    maxHeapify(H, largest);
  }
}

void buildMaxHeap(int H){
  int i;
  for(i = H/2; 0 < i; i--){
    maxHeapify(H, i);
  }
}

void print(int id){
  printf(" %d", Heap[id].key);
}