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

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

int main()
{
  int i,H;
  int *A;
  
  scanf("%d",&H);
  A=(int *)malloc(sizeof(int)*H+1);


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

  buildMaxHeap(A,H);

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

  

  return 0;
}

void maxHeapify(int *A,int i,int H)
{
  int largest,a,l,r;
  l=2*i;
  r=2*i+1;

  if(i>H) return;
  if(H>=l && 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,H);
    }
}

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

}

