#include<stdio.h>
#define MAX 500000
int maxHeap(int);
int A[MAX+1],H;
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(" %d\n",A[i]);
  return 0;
}

int maxHeap(int i){
  int l,r,lgs,k;
  l=2*i;
  r=2*i+1;
  if(l<=H&&A[l]>A[i])lgs=l;
  else lgs=i;
  if(r<=H&&A[r]>A[lgs])lgs=r;
  
  if(lgs!=i){
    k=A[i];
    A[i]=A[lgs];
    A[lgs]=k;
     maxHeap(lgs);
  }
 
  
}

