#include <stdio.h>
#define HEAP_SIZE 500500

int n;

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

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

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

    maxHeapfy(A, largest);
  }
}

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

int main(void) {
  int i;
  int A[HEAP_SIZE];
  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]);
  }

  return 0;
}