#include <stdio.h>
#define M 2000000
int h,a[M+1];
void maxH (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--)maxH(i);
  for(i=1;i<=h;i++)
    {
      printf(" ");
      printf("%d",a[i]);
      if(i==h)printf("\n");
    }


  return 0;
}

void maxH (int i)
{
  int l,r,L,temp;
  l =2*i;
  r =2*i+1;

  if( l<=h && a[l]> a[i])L=l;
  else L =i;
  if(r <=h && a[r]> a[L])L=r;


  if(L !=i){
    temp=a[i];
    a[i]=a[L];
    a[L]=temp;
    maxH(L);
  }
}


  










 

