#include <stdio.h>
#define H 5001
void maxheap(int *, int);
int main(){
  int A[H];
  int i, n;
  scanf("%d", &n);
  for(i=1;i<=n;i++) scanf("%d", &A[i]);

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

void maxheap(int *q, int i){
  int left, right, largest;
  int tmp;
 
  left=2*i;
  right=2*i+1;
  if(q[left] == '\0' || q[right] == '\0')return;
  if(left<=H && q[left]>q[i]) largest = left;
  else largest = i;

  if(right<=H && q[right]>q[largest]) largest = right;
  if(largest != i){
    tmp = q[i];
    q[i] = q[largest];
    q[largest] = tmp;
  }
 
}

