// Heap ii
#include <stdio.h>
#include <stdlib.h>

typedef struct node {
  int key;
  struct node* parent;
  struct node* left;
  struct node* right;
} node_p;

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

int n;

int main() {
  int i;
  node_p* node;

  // input
  scanf("%d", &n);

  // memory allocate
  node = (node_p*)malloc((n+1) * sizeof(node_p));

  // input the key of node
  for(i = 1; i <= n; i++) {
    scanf("%d", &node[i].key);
    if(i == 1) {
      node[i].parent = NULL;
    }
    else {
      if(i % 2 == 0) node[i/2].left = &node[i];
      else node[i/2].right = &node[i/2];
      node[i].parent = &node[i/2];
    }
    node[i].left = NULL;
    node[i].right = NULL;
  }

  buildMaxHeap(node);

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

  return 0;
}

void maxHeapify(node_p* node, int i) {
  int l, r, largest;
  node_p tmp;

  l = 2*i;
  r = 2*i+1;
  // choose the node which has max value
  if((l <= n) && (node[l].key > node[i].key)) {
    largest = l;
  } else {
    largest = i;
  }
  if((r <= n)&&(node[r].key > node[largest].key)) {
    largest = r;
  }
  if(largest != i){ // child of i is bigger than i
    // exchange
    tmp = node[i];
    node[i] = node[largest];
    node[largest] = tmp;
    // recursive
    maxHeapify(node, largest);
  }
}

void buildMaxHeap(node_p *node) {
  int i;
  
  for(i = n/2; i >= 1; i--) {
    maxHeapify(node, i);
  }
}

