#include <stdio.h>
#define H 500000
void maxheap(int *,int,int);
void buildheap(int *,int);

int main()
{
  int A[H];
  int n,i,j,k;
  while(1)
    {
      scanf("%d",&n);
      if(n>=1 && n<=H)
	break;
    }
  for(i=1;i<=n;i++)
    {
      scanf("%d",&A[i]);
    }
  buildheap(A,n);
  for(i=1;i<=n;i++)
    {
      printf(" %d",A[i]);
    }
  printf("\n");
  return 0;
}

void maxheap(int *A,int i,int heapsize)
{
  int l,r,largest,exchange;  
  l=2*i;
  r=2*i+1;
  if(l<=heapsize && A[l]>A[i])
    largest=l;
  else{
    largest=i;
  }
  if(r<=heapsize && A[r]>A[largest])
    largest=r;
  if(largest!=i)
    {
     
      exchange=A[i];
      A[i]=A[largest];
      A[largest]=exchange;
      maxheap(A,largest,heapsize);
    }
}
void buildheap(int *A,int h)
{
  int i;
  for(i=h/2;i>0;i--)
    maxheap(A,i,h);
}

