#include <stdio.h>

int n, heap[2000001];
void maxheapify(int *, int);
void buildmaxheap(int *);

main() {
  int i;
  
  scanf("%d", &n);
  
  for(i = 1; i < n + 1; i++) {
    scanf("%d", &heap[i]);
  }
  
  buildmaxheap(heap);
  
  for(i = 1; i < n + 1; i++) {
    if(i == n) {
      printf(" %d\n", heap[i]);
    }
    else {
      printf(" %d", heap[i]);
    }
    return 0;
  }
}

void maxheapify(int *a, int i) {
  int largest, l, r, tmp;
  l = 2 * i;
  r = 2 * i + 1;
  if(l <= n && a[l] > a[i]) {
    largest = l;
  }
  else {
    largest = i;
  }
  if(r <= n && 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 = (n / 2); i >= 1; i--) {
    maxheapify(a, i);
  }
}