#include<stdio.h>
#define N 500001

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

int main()
{
  int n,i,A[N];
  scanf("%d",&n);
  for(i=1;i<=n;i++)
    scanf("%d",&A[i]);
  buildMaxHeap(A,n);
  for(i=1;i<=n;i++)
    printf(" %d",A[i]);
  printf("\n");

  return 0;
}

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

if(r<=n && A[r] > A[largest])
  largest = r;
 if (largest != i)
   {x=A[i];
    A[i]=A[largest];
    A[largest]=x;

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

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



