#include <stdio.h>
#define N 500001

int A[N];

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

  while(1) {
    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[i];
      A[i] = A[largest];
      A[largest] = tmp;
      i = largest;
    }
    else break;
  }
}

void buildMaxHeap(int h) {
  int i;
  for(i = h/2; i > 0; i--) {
    maxHeapify(i, h);
  }
}

int main() {
  int i, h;

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

  buildMaxHeap(h);

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