#include <stdio.h>

#define NIL 500003;

int n, heap[500000];
void maxheapify(int *, int);
int left(int *, int);
int right(int *, int);
void buildmaxheap(int *);

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

void maxheapify(int *a, int i) {
  int largest, l, r, tmp;
  l = left(a, i);
  r = right(a, i);
  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);
  }
}

int left(int *a, int i) {
  if(n < 2 * i) {
    return NIL;
  }
  else {
    return 2 * i;
  }
}
int right(int *a, int i) {
  if(n < (2 * i + 1)) {
    return NIL;
  }
  else {
    return 2 * i + 1;
  }
}

void buildmaxheap(int *a) {
  int i;
  for(i = (n / 2); i >= 1; i--) {
    maxheapify(a, i);
  }
}