#include<stdio.h>
#define MAX 2000000

int J,S[MAX];
void maxHeap(int);

int main(){
  int i;

scanf("%d",&J);
 for(i=1;i<=J;i++){
   scanf("%d",&S[i]);
 }
 for(i=J/2;i>=1;i--){
   maxHeap(i);
 }
 for(i=1;i<=J;i++){
   printf(" %d",S[i]);
 }
 printf("\n");
 return 0;
}

void maxHeap(int i){
  int a,k,e,largest;
  k=2*i;
  e=2*i+1;
  if(k<=J && S[k] > S[i])largest = k;
  else largest = i;
  if(e <= J && S[e] > S[largest])largest = e;
  if(largest != i){
    a=S[i];
    S[i]=S[largest];
    S[largest]=a;
    maxHeap(largest);
  }
}
