#include <stdio.h>

#define H 500000

int n;

void maxHeapify(int *A, int i) {
  int largest, left = i * 2, right = i * 2 + 1, tmp;

  if ((left <= n) && (A[left] > A[i])) {
    largest = left;
  } else {
    largest = i;
  }
  
  if ((right <= n) && (A[right] > A[largest])) {
    largest = right;
  }
  
  if (largest != i) {
    tmp = A[i];
    A[i] = A[largest];
    A[largest] = tmp;
    maxHeapify(A, largest);
  }
}

void buildMaxHeap(int *A) {
  int i;
  
  for (i = n / 2; i >= 1; i--) {
    maxHeapify(A, i);
  }
}

int main() {
  int i, A[H];

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

  buildMaxHeap(A);

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

  return 0;
}

