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

void maxHeapify(int *,int);
void buildMaxHeap(int *);
int left(int i){return 2*i;}
int right(int i){return 2*i+1;}

int H;

int main()
{
  int *A,i;

  scanf("%d",&H);
  if(H<1 || H>500000) exit(1);

  A = (int *)malloc(sizeof(int)*H*2+1);
  
  for(i=1;i<=H;i++)
    scanf("%d",&A[i]);

  buildMaxHeap(A);

  for(i=1;i<=H;i++)
    printf(" %d",A[i]);
  printf("\n");

  return 0;
}

void maxHeapify(int *A,int i)
{
  int l,r,largest,t;
  
  l = left(i);
  r = right(i);
  //printf("/l=%d,r=%d\n",l,r);

  if(l<=H && A[l]>A[i]) largest = l; //親と左の子の比較
  else largest = i;
  if(r<=H && A[r]>A[largest]) largest = r; //親と右の子の比較
  if(largest != i){ //子が大きければ交換
    t = A[i];
    A[i] = A[largest];
    A[largest] = t;
    maxHeapify(A,largest); 
  }
}

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

