#include<stdio.h>
#define N 500000

int A[N],n;
void maxHeapify(i) {
  
  int left,right,largest,tmp;
  
  left = 2*i;
  right = 2*i+1;
  
 
    
    if(left <= n && (A[left] > A[i]))
      largest = left;
    else largest = i;
    
    if(right <= n && (A[right] > A[largest]))
      largest = right;
    
    if(largest != i) {
      
      tmp = A[i];
      A[i] = A[largest];
      A[largest] = tmp;
      
      maxHeapify(largest);
    
  }
}

void buildMaxHeap(int A[]) {

  int i;

  for(i=n/2;i>=1;i--)
    maxHeapify(i);

}

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