#include <stdio.h>
#include <stdlib.h>
int left(int i){return 2*i;}
int right(int i){return 2*i+1;}
void maxHeapify(int *H,int i,int n){
  int alt,max,l,r;
  l=left(i);
  if(l>n)l=-1;
  r=right(i);
  if(r>n)r=-1;
  if(l!=-1&&l<=n&&H[l]>H[i])max=l;
  else max=i;
  if(r!=-1&&r<=n&&H[r]>H[max])max=r;
  if(max!=i){
    alt=H[i];
    H[i]=H[max];
    H[max]=alt;
    maxHeapify(H,max,n);
  }
}
int main(){
  int i,n;
  int *ar,*H;

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