#include<stdio.h>
#include<stdlib.h>
#define Max 2000000

int H,A[Max+1];
void maxheap(int i){
  int l,r,max,tmp;
  l=2*i;
  r=2*i+1;

  if(l <= H && A[l]>A[i])max=l;
  else max=i;
  if(r<=H && A[r]>A[max])max=r;

  if(max!=i){
    tmp=A[i];A[i]=A[max];A[max]=tmp;
    maxheap(max);
  }
}


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;
}

