#include <stdio.h>
#define N 500000



typedef struct{
  int key, lk, rk, pk;
}Node;

void Insert(int);
void MaxHeapify(int);
void buildMaxHeap();

Node node[N];
int n, size;
	    
int main () {
  int i, m = 0;


  scanf("%d",&n);

  for(i = 1 ; i <= n ; i++) {
    scanf("%d",&m);
    Insert(m);
  }


  buildMaxHeap();

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

  printf("\n");


  return 0;
}


void Insert(int x) {
  int i;

  size++;

  node[size].key = x;
  node[size].lk = size * 2;
  node[size].rk = size * 2 + 1;
  node[size].pk = size / 2;
}


void MaxHeapify(int x) {
  int left = 2 * x, right = 2 * x + 1, large = 0, tmp = 0;


  if(left <= n && node[left].key > node[x].key) large = left;
  
  else large = x;
  
  if(right <= n && node[right].key > node[large].key) large = right;

  if(large != x) {
    tmp = node[x].key;
    node[x].key = node[large].key;
    node[large].key = tmp;
    MaxHeapify(large);
  }
 
}


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

