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

int H;

void maxHeapify(int *A, int i);

int main(int argc, char *argv[])
{
  int i;
  int *A;

  scanf("%d", &H);
  A = (int *)malloc(sizeof(int) * (H + 1));
  for (i = 1; i <= H; i++) scanf("%d", &A[i]);

  for (i = H / 2; i > 0; i--) maxHeapify(A, i);

  for (i = 1; i <= H; i++) printf(" %d", A[i]);
  printf("\n");

  return 0;
}

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

  l = 2 * i;
  r = 2 * i + 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) {
    tmp = A[largest];
    A[largest] = A[i];
    A[i] = tmp;
    maxHeapify(A, largest);
  }
}