#include <stdio.h>
int n, A[500001];

void maxHeap(i) {
  if(i > n / 2)
    return;

  int tmp, largest = i, l = i * 2, r = i * 2 + 1;

  if(A[i] > A[l])
    largest = i;
  else
    largest = l;
  if(A[r] > A[largest])
    largest = r;

  if(A[largest] != A[i]) {
    tmp = A[largest];
    A[largest] = A[i];
    A[i] = tmp;
    maxHeap(largest);
  }
}

void buildMaxHeap() {
  int i;
  for(i = n / 2;i > 0;i--)
    maxHeap(i);
}

int main() {
  int i;

  scanf("%d", &n);
  for(i = 1;i < n + 1;i++)
    scanf("%d", &A[i]);

  buildMaxHeap();

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

  return 0;
}

