#include<stdio.h>

int H;

void maxHeapify(int* A, int i){
  int left,right,largest,temp; 
  left = 2*i; 
  right = 2*i+1;
      
  if(left <= H && A[left] > A[i]) largest = left;
  else largest = i;
  
  if(right <= H && A[right] > A[largest])largest = right;

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

void buildMaxHeap(int* A){
  int i;
  for(i=H/2;i>0;i--) maxHeapify(A, i);
}

int main(){
  int i,A[500000];
  scanf("%d",&H);
  for(i=1;i<H+1;i++) scanf("%d",&A[i]);

  buildMaxHeap(A);

  for(i=1;i<H+1;i++) printf(" %d",A[i]);
  printf("\n");
  
  return 0;
}

