#include<stdio.h>
#define M 5000000

int H,A[M+1];
void maxheap(int i);

int main(){
  int i;
  
  scanf("%d",&H);

  for(i=1;i<=H;i++) scanf("%d",&A[i]);
  for(i=H/2;i>=1;i--) maxheap(i);
  for(i=1;i<=H;i++){
    printf(" %d",A[i]);
  }
  printf("\n");
  return 0;
}
  
    
void maxheap(int i){
  int left,right,saidai,x;
  left=2*i;
  right=2*i+1;

  if(left<=H && A[left] > A[i])
    saidai=left;
  else saidai=i;

  if(right<=H && A[right] > A[saidai])
    saidai=right;

  if(saidai!=i){
    x=A[i];
    A[i]=A[saidai];
    A[saidai]=x;
    maxheap(saidai);
  }
}

