#include<stdio.h>
#define MAX 500000
int A[MAX + 1],n;

void Max(int i);

void build();

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



void Max(int i){
  int max, t, right , left;
  
  left =2 * i;
  right = 2 * i + 1;
  if(left <= n && A[left] > A[i]){
    max = left;
  }
  else {
    max = i;
  }
  if(right <= n && A[right]>A[max]){
    max = right;
  }
  if(max != i){
    t=A[i];
    A[i]=A[max];
    A[max]=t;
    return Max(max);
 
  }
}


void build(){
  int i;
  for(i = n / 2 ; i >= 1 ;i--){
    Max(i);
  }
}

