#include<stdio.h>
#define MAX 2000000

int H,a[MAX+1];

void maxheap(int i){
  int l,r,largest,t;
  l=2*i;
  r=2*i+1;

  if(l<=H && a[l]>a[i])largest=l;
  else largest=i;
  if(r<=H && a[r]>a[largest])largest=r;

  if(largest!=i){
    t=a[i];
    a[i]=a[largest];
    a[largest]=t;
    maxheap(largest);
  }
}

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