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

int Parent(int );
int Left(int );
int Right(int );

void maxHeapify(int * ,int );
void buildMaxHeap(int *);

int H; //heap-size = n
int main() {
  int n;
  int i;
  int j;
  int *array;

  scanf("%d",&n);
  H = n;
  array = (int *)malloc(n * sizeof(int));
  for(i=0;i<n;i++) scanf(" %d",&array[i]);

  buildMaxHeap(array);

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

int Parent(int i) {
  return i/2;
}

int Left(int i) {
  return 2*i;
}

int Right(int i) {
  return 2*i + 1;
}

void maxHeapify(int *A,int i) {  //i->+1
  int l,r;
  int largest;
  int tmp;
  l = Left(i); //l->+1
  r = Right(i); //r->+1
  // 左の子、自分、右の子で値が最大のノードを選ぶ
  if(l > H || r > H) largest = i;
  else {
    if(l <= H && A[l-1] > A[i-1]) largest = l;  //largest->+1
    else largest = i; //largest->+1
    if(r <= H && A[r-1] > A[largest-1]) largest = r; //largest->+1
  }

  //printf("checklargest %d:%d\n",largest,A[largest-1]);

  //printf("%d : i->%d, left->%d, right->%d\n",i,A[i-1],A[l-1],A[r-1]);
  if(largest != i) {
    tmp = A[i-1];
    A[i-1] = A[largest-1];
    A[largest-1] = tmp;

    maxHeapify(A, largest); //
  }
  
}

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

