#include <stdio.h>
#include <stdlib.h>
#define NIL NULL
int A[500001],h;

void maxHeapify(int i){
  int l=(i*2);
  int r=(i*2+1);
  int largest,temp;
  if(l<=h && A[l]>A[i])largest=l;
  else largest=i;
  if(r<=h && A[r]>A[largest])largest=r;
  if(largest!=i){
    temp=A[i];
    A[i]=A[largest];
    A[largest]=temp;
    maxHeapify(largest);
  }
}

int main(){
  int i, j;

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


