#include<stdio.h>
#include<math.h>
#define N 500000
void maxHeapify(int *,int,int);
//void builtMaxHeap(int *,int,int);
int main()
{
  int i,A[N+1],n,a;
  scanf("%d",&n);
  for(i=1;i<=n;i++)
    {
      scanf("%d",&A[i]);
    }
  a=n/2;
   for(i=a;i>=1;i--)
    {
     
      maxHeapify(A,i,n);
    }
   for(i=1;i<=n;i++)
     {
       printf(" %d",A[i]);
     }
   printf("\n");
   return 0;
}

void maxHeapify(int A[],int i,int n)
{
  int b,c,l,r,largest;
  l = 2*i;
  r = 2*i+1;
  if(l<=n && A[l] >A[i])largest = l;
  else largest = i;
  if( r <= n && A[r] > A[largest] )largest = r;
  if(largest !=i)
    {
      b = A[i];
      c = A[largest];
      A[largest] = b;
      A[i] = c;
      
      maxHeapify(A,largest,n);
    }
}

/*void builtMaxHeap(int A[],int a,int n)
{
  int i;
  for(i=a;i>1;i--)
    {
      maxHeapify(A,i,n);
    }
}
*/

