#include<stdio.h>

int T[5000001],H;
int parent(int i){
  return i/2;
}
int left(int i){
  return i*2;
}
int right(int i){
  return i*2+1;
}
void maxHeapify(int *T,int i){
  int l,r,largest,tmp;
  l = left(i);
  r = right(i);
  if(l<=H && T[l]>T[i]){
    largest = l;
  }
  else largest = i;
  if(r<=H && T[r] > T[largest]){
    largest = r;
  }
  if(largest != i){
    tmp = T[i];
    T[i] = T[largest];
    T[largest] = tmp;
    maxHeapify(T,largest);
  }
}
void buildMaxheap(int *T){
  int i;
  for(i=H/2;i>0;i--){
    maxHeapify(T,i);
  }
}


int main(){
  int i,n;
  scanf("%d",&H);
  for(i=1;i<=H;i++){
    scanf("%d",&T[i]);
  }
  buildMaxheap(T);
  for(i=1;i<=H;i++){
    printf(" %d",T[i]);
  }
  return 0;
}

