#include <stdio.h>

#define N 500001

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

int main() {
  int h, i, data[N];

  scanf("%d", &h);

  for (i = 1; i <= h; i++) {
    scanf("%d", &data[i]);
  }

  buildMaxHeap(data, h);

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

  return 0;
}


void maxHeapify(int *d, int i, int h) {
  int l, r, largest, temp;
  
  l = i * 2;
  r = i * 2 + 1;

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

  if (r <= h && d[r] > d[largest]) largest = r;

  if (largest != i) {
    temp = d[i];
    d[i] = d[largest];
    d[largest] = temp;
    maxHeapify(d, largest, h);
  }
}


void buildMaxHeap(int *d, int h) {
  int i ,n;

  n = h / 2;

  for (i = n; i > 0; i--) {
    maxHeapify(d, i, h);
  }
}

