#include<stdio.h>

int n,S[10000000];


void maxHeapify(int i){
  int l, r,x,largest;

  l = left(i);
  r = right(i);
    if(l <= n && S[l] > S[i]){
    largest = l;
  }
  else largest = i;
  if(r <= n && S[r] > S[largest]){
    largest = r;
  }
  if(largest != i){
    x = S[i];
    S[i] = S[largest];
    S[largest] = x;
    maxHeapify(largest);
  }
  
}



int parent(int i){
  return i/2;
}
int left (int i){
  return 2*i;
}
int right(int i){
  return 2*i+1;
}
  
int main(){
  int i;

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

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




  
  for(i=1;i<=n;i++){
    printf(" %d",S[i]);
  }
  printf("\n");

  return 0;
}