#include <stdio.h>

int key[500001],h;
int parent(int i)
{
  return i/2;
}
int left(int i)
{
  return 2*i;
}
int right(int i)
{
  return 2*i+1;
}
void max(int i)
{
  int largest,tmp;
  int l = left(i);
  int r = right(i);
  if(l<=h&&key[l]>key[i]) largest = l;
  else largest = i;
  if(r<=h&&key[r]>key[largest]) largest = r;
  if(largest!=i)
  {
    tmp=key[i];
    key[i]=key[largest];
    key[largest]=tmp;
    max(largest);
  }
}
void build()
{
  int i;
  for(i=h/2;i>0;i--)max(i);
}

int main()
{
  int i;
  scanf("%d",&h);
  for(i=1;i<=h;i++)
  {
    scanf("%d",&key[i]);
  }
  build();
  for(i=1;i<=h;i++)
  {
    printf(" %d",key[i]);
  }
  printf("\n");
  return 0;
}

