#include <stdio.h>
#define Max 2000000

int H,A[Max+1];


void maxHeapfy(int n){
  int left,right,largest;

  left=2*n;
  right=2*n +1;

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

  if(largest != n){
     int x =A[n];
     A[n] = A[largest];
     A[largest] = x;
     maxHeapfy(largest);
  }
}

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

  for(i=1;i<=H;i++) scanf("%d",&A[i]);
  for(i=H/2;i>=1;i--) maxHeapfy(i);
  for(i=1;i<=H;i++) printf(" %d",A[i]);
  
  printf("\n");

  return 0;
}



