#include<stdio.h>
#define NIL 500003
 
int A[500001],n;
 
int main(){
  int i;
  scanf("%d",&n);
  for(i=1;i<=n;i++){
   scanf("%d",&A[i]);
  }
  
  for(i=n+1;i<500001;i++){
  A[i] = NIL;
  }
  for(i=n/2;i>=1;i--){
  
   maxHeapify(i);
  }
  for(i=1;i<=n;i++)
    {
  printf(" %d",A[i]);
    }  
printf("\n");
  
}

int left(int i){
return i*2;
}

int right(int i){
return i*2+1;
}

void exchange(int *a,int *b){
  int tmp=*a;
  *a=*b;
  *b=tmp;
}
 
void maxHeapify(int i){
  
  int l;
  int r;
  int largest;

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