#include <stdio.h>

int heaps[500010], h;

void max_heapify(int a[], int i) {
  int largest, b;
  int l = i * 2;
  int r = i * 2 + 1;

  if (l <= h && a[l] > a[i])
    largest = l;
  else
    largest = i;
  if (r <= h && a[r] > a[largest])
    largest = r;

  if (largest != i) {
    b = a[i];
    a[i] = a[largest];
    a[largest] = b;
    max_heapify(a, largest);
  }
}

void build_maxheap(int a[]) {
  for (int i = h / 2; i >= 1; i--) {
    max_heapify(a, i);
  }
}

int main(void) {

  scanf("%d", &h);
  for (int i = 1; i <= h; i++)
    scanf("%d", &heaps[i]);

  build_maxheap(heaps);

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

