#include <stdio.h>

void maxh(int* ,int);

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

  return 0;
}
void maxh(int a[], int i){
  int l,r,e,largest;        


  l = 2*i;
  r = 2*i+1;
  if (l <= num && a[l] > a[i])
    largest = l;
  else 
    largest = i;
  if (r <= num && a[r] > a[largest])
    largest = r;
  if (largest != i){
    e=a[i]; 
    a[i]=a[largest];         
    a[largest]=e;    
maxh(a, largest);
  }

} 