#include <stdio.h>

void MaxH(int *,int);

void BMax(int *);

int n;

int main()
{
  int i;

  scanf("%d",&n);

  int A[n+1];

  for(i=1;i<=n;i++)
    {
      scanf("%d",&A[i]);
    }

  BMax(A);

  for(i=1;i<=n;i++)printf(" %d",A[i]);


  printf("\n");
  
  return 0;
}

void MaxH(int *A,int i)
{
  int L,R,lg,koukan;

  L=2*i;
  R=2*i+1;

  if(L<=n&&A[L]>A[i])lg=L;
  else lg=i;

  if(R<=n&&A[R]>A[lg])lg=R;

  if(lg!=i)
    {
      koukan=A[i];
      A[i]=A[lg];
      A[lg]=koukan;

      MaxH(A,lg);
    }
}

void BMax(int *A)
{
  int i;
  for(i=n/2;i>=1;i--)
    {
      MaxH(A,i);
    }
}

