#include<stdio.h>

int A[500001],n;

void   maxHeapify(int k)
{
  int l,r,largest,x,y;
  l=2*k;
  r=2*k+1;
  if(l<=n&&A[l]>A[k]) largest=l;
  else largest=k;
  if((r<=n)&&(A[r]>A[largest])) largest=r;

  if(largest!=k)
    {
      x=A[k];
      y=A[largest];
      A[k]=y;
      A[largest]=x;
      maxHeapify(largest);
    }
}

void buildMaxHeap(void)
{
  int i;
  for(i=n/2;i>=1;i--)
    {
      maxHeapify(i);
    }
}


int main()
{
  int i,k=0;

  scanf("%d",&n);

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

  buildMaxHeap();

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

  return 0;
}
