//2020.1.16
//s1270188 xxxmk2
//alds1_09b

#include<stdio.h>
#define N 500000

//prototype
void maxheap(int);

//global
int data[N+1], num;

int main(){
  int i;

  //input
  scanf("%d",&num);
  for(i=1;i<=num;i++) scanf("%d",&data[i]);

  //max heap
  for(i=num/2;i>0;i--) maxheap(i);

  //output
  for(i=1;i<=num;i++) printf(" %d",data[i]);
  printf("\n");
  
  return 0;
}

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

  //printf("\t%d %d %d",data[i],data[l],data[r]);
    
  if(l<=num && data[l]>data[i]) max=l;
  else max=i;
  if(r<=num && data[r]>data[max]) max=r;

  //printf("\t%d\n",data[max]);
  
  if(max!=i){
    l=data[i];
    data[i]=data[max];
    data[max]=l;

    maxheap(max);
  }
}

