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