#include <stdio.h>

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

int h;

int main(){
  int i,H[500001];

  scanf("%d",&h);

  for(i=1;i<=h;i++){
    scanf("%d",&H[i]);
  }

  buildMaxHeap(H);

  /* output */
  for(i=1;i<=h;i++){
    printf(" %d",H[i]);
  }
  printf("\n");

  return 0;
}

void maxHeapify(int *A, int i){
  int l,r,largest,B;
  l = 2*i;
  r = 2*i+1;
  // 左の子、自分、右の子で値が最大のノードを選ぶ
  if(l <= h && A[l]>A[i]) largest=l;
  else largest = i;
  if(r <= h && A[r]>A[largest]) largest = r;
  if(largest!=i){
    B=A[i];
    A[i]=A[largest];
    A[largest]=B;

    maxHeapify(A, largest); // 再帰的に呼び出し
  }
}

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