#include<stdio.h>
#define NIL NULL

int id,T[500001];
void maxHeapify(int );

int main()
{
  int i;
  
  scanf("%d",&id);
  
  for(i = 1;i <= id;i++)
    {
      scanf("%d",&T[i]);
    }
  for(i = id/2;i >= 1;i--)
    {
      maxHeapify(i);
    }
  for(i = 1;i <= id;i++)
    {
      printf(" %d",T[i]);
    }
  printf("\n");
  return 0;
}
void maxHeapify(int i)
{
  int a,r,l,largest;
  l = 2*i;
  r = (2*i) + 1;
  if(l <= id && T[l] > T[i])
    {
      largest = l;
    } else
    {
      largest = i;
    }
  if(r <= id && T[r] > T[largest])
    {
      largest = r;
    }
  if(largest != i)
    {
      a = T[i];
      T[i] = T[largest];
      T[largest] = a;
      maxHeapify(largest);
    }
}

