#include<stdio.h>

int maxHeapify(int);

int array[500001];
int i,num,num2,l,r,kind=0;
  
int main(void){

  scanf("%d",&num);

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

  
int maxHeapify(i){
 r = 1 +  2 * i;
  l = 2 * i;

  if(l <= num && array[l]>array[i])num2=l;

  else num2 = i;

  if(r <= num && array[r]>array[num2])num2=r;

  if(num2 != i){
    kind = array[i];
    array[i] = array[num2];
    array[num2] = kind;
    return maxHeapify(num2);
  }

  return 0;
}