#include<stdio.h>
#include<stdlib.h>
#define H 500000
int A[H];

void maxHeapify (int i, int m){
  int l, r, larg, n;

  l = i*2;
  r = i*2+1;

  if(l <= m && A[l] > A[i])larg = l;
  else larg = i;

  if(r <= m && A[r] > A[larg]){
    larg = r;
  
  }
  
  if(larg != i){
    n = A[i]; 
    A[i] = A[larg];
    A[larg] = n;
    maxHeapify(larg, m);
    }
}


  
int main(){
 
    int n, i;
  
  scanf("%d",&n);

  for(i=1 ; i<=n ; i++){
    scanf("%d",&A[i]);
    //printf("%d",A[i]);
  }

 
  for(i = n/2 ; i>0 ;i--)maxHeapify(i,n);
 
  
  for(i=1 ; i<=n ; i++){

    printf(" %d",A[i]);
    
  }
  printf("\n");
  
  
  return 0;
}

