#include <stdio.h>
int n,H[500000];


void maxHeapify( int i )
{
  int l,r,large,tmp;
  l=2*i;
  r=2*i+1;

  if(l<= n && H[l] > H[i]) large=l;
  else large=i;

  if(r<= n && H[r] > H[large]) large=r;

  if(large!=i){
    tmp=H[i];
    H[i]=H[large];
    H[large]=tmp;
  

  maxHeapify(large);
  }
}
     


void buildMaxHeap(){
  int i;
  for(i=n/2;i>0;i--){
    maxHeapify(i);
  }
}



int main(){

  int i;
  scanf("%d",&n);

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

  return 0;

}