#include<stdio.h>
int H;
void maxHeapify(int A[],int i)
{
  int l,r,largest,a;
  l=i*2;
  r=l+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)
        {
          a=A[i];
          A[i]=A[largest];
          A[largest]=a;
      maxHeapify(A,largest);
        }
}
void buildMaxHeap(int A[])
{
  int i;
  for(i=H/2;i>=1;i--)
    {
      maxHeapify(A,i);
    }
}
 

int main()
{
  int i,A[500000];
   scanf("%d",&H);
      for(i=1;i<=H;i++)
    {
      scanf("%d",&A[i]);
    }
      buildMaxHeap(A);

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

  return 0;
}

