#include <stdio.h>
int H[500000];
int n;
void maxHeapify(int i){

  int l,r;
  int largest,tmp;
  l=left(i);
  r=right(i);

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

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

  return 0;
}