#include<stdio.h>

int n, A[2000001];

void maxHeapify(int a){
  int left,right,largest;
  int tmp;
  
  left = 2*a;
  right = 2*a+1;

  if(left <= n && A[left]>A[a]) largest=left;
  else largest=a;

  if(right<=n&&A[right] >A[largest]) largest=right;

  if(largest!=a){
    tmp=A[a];
    A[a]=A[largest];
    A[largest]=tmp;
    maxHeapify(largest);
  }
  
}

int main(){
  int i;

  scanf("%d",&n);

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

  printf("\n");
  
  return 0;
}

