#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef int HeapElementType;
typedef bool (*CompareFunc)(HeapElementType, HeapElementType);

void heapify(HeapElementType*, int, CompareFunc, int);
void buildHeap(HeapElementType*, int, CompareFunc);
bool compare(HeapElementType, HeapElementType);

int main () {
  int H;
  HeapElementType *heap = NULL;
  
  scanf("%d", &H);
  heap = (HeapElementType*)malloc(sizeof(HeapElementType)*(H+1));
  for (int i = 1; i <= H; ++i) {
    scanf("%d", &heap[i]);
  }

  buildHeap(heap, H, compare);

  for (int i = 1; i <= H; ++i) {
    printf(" %d", heap[i]);
  }
  printf("\n");
  
  return 0;
}

bool compare(HeapElementType x, HeapElementType y) {
  return x > y;
}

void heapify(HeapElementType* heap, int H, CompareFunc cmp, int i) {
  int left  = 2*i;
  int right = 2*i + 1;

  int target;
  if (left <= H && cmp(heap[left], heap[i])) {
    target = left;
  } else {
    target = i;
  }

  if (right <= H && cmp(heap[right], heap[target])) {
    target = right;
  }

  if (target != i) {
    HeapElementType t = heap[i];
    heap[i] = heap[target];
    heap[target] = t;
    heapify(heap, H, cmp, target);
  }
}

void buildHeap(HeapElementType* heap, int H, CompareFunc cmp) {
  for (int i = H/2; i >= 1; --i) {
    heapify(heap, H, cmp, i);
  }
}

