#include <stdio.h>
#define nil -1
#define max 500001
typedef struct{
  int key;
  int parent;
  int right;
  int left;
} node;
int parent(int);
int left(int);
int right(int);
void PrintData(void);
void buildMaxHeap(void);
void maxHeapify(int);
int H;
node data[max];

int main()
{
  int i;

  scanf("%d",&H);

  for(i = 0 ; i < H + 1 ; i++){
    data[i].parent = nil;
    data[i].right = nil;
    data[i].left = nil; }
  
  for(i = 1 ; i < H + 1 ; i++){
    scanf("%d",&data[i].key);
    if(i != 1) data[i].parent = parent(i); 
    if(left(i) < H + 1) data[i].left = left(i);
    if(right(i) < H + 1) data[i].right = right(i); }

  buildMaxHeap();
  
  PrintData();
  
  return 0;
}

int parent(int i)
{
  return i/2;
}

int left(int i)
{
  return 2*i;
}

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

void PrintData(void)
{
  int i;	

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

void buildMaxHeap(void)
{
  int i;

  for(i = H/2 ; i >=1 ; i--) maxHeapify(i);

}

void maxHeapify(int i)
{
  int l, r, largest, ForKeep;

  l = left(i);
  r = right(i);

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

  if(r <= H && data[r].key > data[largest].key) largest = r;
  
  if(largest != i){
    ForKeep = data[i].key;
    data[i].key = data[largest].key;
    data[largest].key = ForKeep;
    maxHeapify(largest); }
}

