#include<stdio.h>
#include<stdlib.h>
int *t,n;

void maxheapify(int i){
  int l=2*i,r=2*i+1,large,v;
  if(l<=n&&t[l]>t[i])large=l;
  else large=i;
  if(r<=n&&t[r]>t[large])large=r;
  if(large!=i){
    v=t[i];
    t[i]=t[large];
    t[large]=v;
    maxheapify(large);
  }
}

int main(){
  int i;
  scanf("%d",&n);
  t=(int*)malloc(n*sizeof(int));
  for(i=1;i<=n;i++)scanf("%d",&t[i]);
  for(i=n/2;i>=1;i--)maxheapify(i);
  for(i=1;i<=n;i++)printf(" %d",t[i]);
  printf("\n");
  return 0;
}
    

