#include <stdio.h>
#define MAX 2000000

int H,A[MAX+1];

void heap(int);

int main()
{
  int i;
  scanf("%d",&H);
  for(i=1;i<=H;i++)
    scanf("%d",&A[i]);
  for(i=H/2;i>=1;i--)
    heap(i);
  for(i=1;i<=H;i++)
    printf(" %d",A[i]);
  printf("\n");
}

void heap(int u)
{
  int l = 2 * u,r = 2 * u + 1,max,k;

  if(l <= H && A[l] > A[u])
    max = l;
  else
    max = u;
   if(r <= H && A[r] > A[max])
     max = r;


  if(max != u)
    {
      k=A[u];
      A[u] = A[max];
      A[max] = k;
      heap(max);
    }
}

