#include<stdio.h>

#define S 500000
#define N1 -2000000000
#define N2 2000000000

void maxHeapify(int );
void buildMaxHeap(void);
void Print(void);

int A[S],n;

main(){ 
  int i;

  scanf("%d",&n);
  if(S < n) return 0;

  for(i=1;i <= n;i++){
    scanf("%d",&A[i]);
     if(A[i] < N1 || N2 < A[i]) return 0;
  }
  
  buildMaxHeap();

  Print();

  return 0;
}

void maxHeapify(int i){
  
  int l =0;
  int r = 0;
  int largest =i;
  int a = 0;
  int b = 0;
  
  if(2*i >0 && 2*i <= n) l = 2*i;
  if((2*i+1) <= n) r = 2*i+1;
  
  if(l <= n && A[l] > A[i] && l > 0) largest = l;
  if(r <= n && A[r] > A[largest] && r >0) largest = r;

  if(largest != i){
    a = A[i];
    b = A[largest];
    A[i] = b;
    A[largest] = a;
    
    maxHeapify(largest);
  }
}

void buildMaxHeap(void){

  int i;
  for(i=n/2; i >= 1;i--){
    maxHeapify(i);
  }
}


void Print(void){
  int i;

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