#include<stdio.h>
#include<stdlib.h>
typedef struct node{
  int key;
  struct node *p, *l, *r;
}T_node;
void mahpy(T_node ** ,int);
int main(){
  int n;
  scanf("%d", &n);
  T_node **heap;
  heap = (T_node **)malloc(sizeof(T_node *) * (n + 1));
  int i;
  for(i = 1; i <= n; i++){
    heap[i] = (T_node *)malloc(sizeof(T_node));
    heap[i]->p = heap[i]->l = heap[i]->r = NULL;
    scanf("%d", &(heap[i]->key));
    if(i /2){
      heap[i]->p = heap[i / 2];
      if(i % 2)
	heap[i / 2]->r = heap[i];
      else
	heap[i / 2]->l = heap[i];
    }
  }
  for(i = n / 2; i > 0; i--)
    mahpy(heap, i);
  for(i = 1; i <= n; i++){/*
    printf("node %d: key = %d, ", i, heap[i]->key);
    if(heap[i]->p != NULL)
      printf("parent key = %d, ", heap[i]->p->key);
    if(heap[i]->l != NULL)
      printf("left key = %d, ", heap[i]->l->key);
    if(heap[i]->r != NULL)
      printf("right key = %d, ", heap[i]->r->key);
      printf("\n");*/
    printf(" %d", heap[i]->key);
  }
  printf("\n");
  for(i = n; i > 0; i--)
    free(heap[i]);
  free(heap);
  return 0;
}

void mahpy(T_node **heap, int i){
  T_node *max;
  int tmp, ni = 0;
  if(heap[i]->r != NULL){
    if(heap[i]->l->key >= heap[i]->r->key){
      max = heap[i]->l;
      ni = i * 2;
    }
    else{
      max = heap[i]->r;
      ni = i * 2 + 1;
    }
  }
  else if(heap[i]->l != NULL){
    max = heap[i]->l;
    ni = i * 2;
  }
  else
    max = heap[i];
  if(max->key > heap[i]->key){
    tmp = heap[i]->key;
    heap[i]->key = max->key;
    max->key = tmp;
  }
  if(ni)
    mahpy(heap, ni);
}