#include <stdio.h>


void maxHeapify(int A[],int i,int H)
{
  int l,r,largest;
  int k=0;
  l=2*i;
  r=2*i+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)
    {
      k=A[i];
      A[i]=A[largest];
      A[largest]=k;

      maxHeapify(A,largest,H);
    }
}

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

int main()
{
  int p=0;
  int a[1000000];
  int i;
  scanf("%d",&p);
  for(i=1;i<=p;i++)
    {
      scanf("%d",&a[i]);
    }
  buildMaxHeap(a,p);
  for(i=1;i<=p;i++)printf(" %d",a[i]);
  printf("\n");
  return 0;
}

