#include <stdio.h>
#define H 500000
int key[H];

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

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

