#include<stdio.h>
void maxheap(int *a, int i,int n);
int main()
{
  int n,i;
  scanf("%d",&n);
  int a[n+1];
  //int *a;
  for(i=1;i<=n;i++)scanf("%d",&a[i]);
  for(i=n/2;i>0;i--)maxheap(a,i,n);
  for(i=1;i<=n;i++)printf(" %d",a[i]);
  printf("\n");
  return 0;
}
void maxheap(int *a, int i,int n){
  int r,l,larg,tmp;
  l=2*i;
  r=2*i+1;
  if(l<=n && a[l]>a[i])larg=l;
  else larg=i;
  if(r<=n && a[r]>a[larg])larg=r;
  if(larg!=i){
    tmp=a[larg];
    a[larg]=a[i];
    a[i]=tmp;
    maxheap(a,larg,n);
  }
}
