#include <stdio.h>
#define MAX 10000
int h;

void maxHeapify(int *a, int i) {
  int tmp, largest = 0;
  int left = 2*i;
  int l = left;
  int right = 2*i + 1;
  int r = right;

  //????????????????????????????????§???????????§????????????????????¶
  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;
    maxHeapify(a, largest);
  }
}

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


int main(int argc, char const *argv[]) {
  /* code */
  int  a[MAX+1], i, n;
  scanf("%d", &h);
  for(i=1;i<=h;i++) {
    scanf("%d", &a[i]);
  }
  buildMaxHeap(a);
  for(i=1;i<=h;i++) {
    printf(" %d", a[i]);
  }
  printf("\n");


  return 0;
}