include<stdio.h>
#define N 500000
 
int n,A[N];
 
int main(){
    int i;
 
    scanf("%d",&n);
    for(i = 1;i <= n; i++){
      scanf("%d",&A[i]);
    }
    for(i = n/2;i >= 1;i--){
      maxHeapify(i);
    }
    for(i = 1;i <= n;i++){
      printf(" %d",A[i]);
    }
    printf("\n");
    return 0;
  }

void maxHeapify(int i){
  int c;
  int l = 2 * i;
  int r = 2 * i + 1;
  int max=0;
  if(l <= n && A[l] > A[i]){
    max = l;
  }
  else {
    max = i;
  }
 
  if(r <= n && A[r] > A[max]){
    max = r;
  }
 
  if(max != i){
    c = A[i];
    A[i] = A[max];
    A[max] = c;
    maxHeapify(max);
  }
}
 