#include<stdio.h>
#define N 500000
 
int n;
int p(int i){
  return i/2;
}

int l(int i){
  return 2*i;
}
 
int r(int i){
  return 2*i+1;
}   
 
 
void maxheapify(int h[],int i){
  int largest=0,aa,le,ri;
  le = l(i);
  ri = r(i);
  if(le <= n && h[le] > h[i]){
    largest = le;
  }
  else{
    largest = i;
   }
  if(ri <= n && h[ri] > h[largest]){
    largest = ri;
  }
  if(largest != i){
    aa = h[i];
    h[i] = h[largest];
    h[largest] = aa;
    maxheapify(h,largest);
  }
}

 void buildMaxHeap(int  h[]){
  int i;
  for(i = (n/2); i > 0; i--){
    maxheapify(h,i);
  }
}
   
 
int main(){
  int i;
  int S[N];
  scanf("%d",&n);
  for(i = 1;i <= n;i++){
    scanf("%d",&S[i]);
  }
  
  buildMaxHeap(S);
  
  for(i = 1;i <= n;i++){
    printf(" %d",S[i]);
  }
  printf("\n");
  return 0;
}