#include<stdio.h>
#define MAX 500000
 
int n;

 
void MaxHeapify(int He[],int i){
  int l, r, large;
  int tmp;
   
  l = left(i);
  r = right(i);
 
  if(l<=n && He[l]>He[i]){
    large = l;
  }
  else large = i;
 
  if(r<=n && He[r]>He[large]){
    large = r;
  }
 
  if(large != i){
    tmp = He[i];
    He[i] = He[large];
    He[large] = tmp;
 
    MaxHeapify(He, large);
  }  
}

void BuildMaxHeap(int He[]){
  int i;
  for(i=n/2; i >= 1;i--){
    MaxHeapify(He,i);
  }
}
 

int left(int l){
  int x;
  x = 2*l;
  return x;
};
int right(int r){
  int x;
  x = 2*r+1;
  return x;
};

int main(){
  int i;
  int He[MAX];
 
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d",&He[i]);
  }
   
  BuildMaxHeap(He);
 
  for(i=1;i<=n;i++){
    printf(" %d",He[i]);
  }
  
  printf("\n");
   
  return 0;
}