#include <stdio.h>
#include <stdlib.h>

#define H 500001

typedef struct {
  int k;
} node;

void maxHeapify (node *,int);
void swap (node *,node *);

int size;

int main(int argc, char const *argv[]) {

  int i,j;


  node* n = (node*) malloc(sizeof(node) * H);
  scanf("%d",&size);

for (i = 1; i <= size; i++) {
  scanf("%d",&n[i].k);

}

for (i = (size/2) ; i >= 1; i--) {
  maxHeapify(n,i);
}

for (i = 1; i <= size; i++) {
    printf(" %d",n[i].k);
}
  printf("\n");

  free(n);

  return 0;
}

void maxHeapify (node *n, int i){
  int largest;
  int l = i*2;
  int r = i*2+1;

  if (l <= size && n[l].k > n[i].k) {
    largest = l;
  }

  else{
    largest = i;
  }

  if (r <= size && n[r].k > n[largest].k) {
    largest = r;
  }

  if (largest != i) {
    swap (&n[i],&n[largest]);
    maxHeapify (n, largest);
  }

}

void swap(node *x, node *y){
  node temp;
  temp = *y;
  *y = *x;
  *x = temp;
}