#include <stdio.h>
#include <stdlib.h>
void maxHeapify(int *,int ,int);
int main() {
  int i,n;
  int *H;
  scanf("%d",&n);
  H = malloc((n+1)*sizeof(int));
  for(i=1;i<=n;i++)
    {
      scanf("%d",&H[i]);
    }
  
   for(i=n/2;i>0;i--)
     {
       maxHeapify(H,i,n);
     }

   for(i=1;i<=n;i++)
    {
      printf(" %d",H[i]); 
    }
   printf("\n");
   
  return 0;
}
void maxHeapify(int *A,int i, int n)
{
  int j,largest;
  int emp;

  if(2*i <= n && A[2*i] > A[i]) largest = 2*i;
  else largest = i;
  if((2*i)+1 <= n && A[2*i+1] > A[largest]) largest = (2*i)+1;
  if(largest != i)
    {
      emp = A[largest];
      A[largest]= A[i];
      A[i] = emp;
      maxHeapify(A,largest,n);
    }

}

