#include <stdio.h>
#include <stdlib.h>
 
void maxHeap(int *, int, int);

int main(void){
  int i, num;
 int H[500000];
  scanf("%d", &num);
 
  for(i = 1;i <= num;i++) scanf("%d", &H[i]);
 
  for(i = num/2;i > 0;i--) maxHeap(H, i, num);
 
 
  for(i = 1;i <= num;i++) printf(" %d", H[i]);
 
  printf("\n");
  return 0;
}
void maxHeap(int *H, int i, int num){
  int l, r, large, c;
 
  if( i*2+1 < num + 1)
    { 
    r = i*2+1;
    }
  else r = i;
  if( i*2 < num + 1) 
    {
      l = i*2;
    }
  else l = i;
 
  if(l <= num && H[l] > H[i])
    {
      large = l;
    }
      else large = i;
    
  if(r <= num && H[r] > H[large])
    {
    large = r;
    }
  if(large != i)
    {
    c = H[i];
    H[i] = H[large];
    H[large] = c;
    maxHeap(H, large, num);
    }
}