#include<stdio.h>
#include<stdlib.h>

void maxHeapify(int *,int,int);

int main(){
  int i;
  int H;
  int *A;

  scanf("%d",&H);
  H++;
  A=(int *)malloc(H*sizeof(int));

  for(i=1;i<H;i++){
    scanf("%d",&A[i]);
  }
  for(i=H/2;i>0;i--){
    maxHeapify(A,i,(H-1));
  }


  for(i=1;i<H;i++){
    printf(" %d",A[i]);
  }
  printf("\n");
return 0;
}
void maxHeapify(int *A,int i,int H){
  int l;
  int r;
  int largest;
  int temp;
  l=i*2;
  r=i*2+1;
  if(l<=H&&A[l]>A[i]) largest=l;
  else largest=i;
  
  if(r<=H&&A[r]>A[largest]) largest=r;

  if(largest!=i){
    temp=A[i];
    A[i]=A[largest];
    A[largest]=temp;
    maxHeapify(A,largest,H);
  }
  return;
}
    

