#include<stdio.h>

void Max_h(int);
void build();

int n;
int Key[1000000];

int main(){
  int i;

  scanf("%d", &n);
  for(i = 1; i <= n; i++){
    scanf("%d", &Key[i]);
  }

  build();

  for(i = 1; i <= n; i++){
    printf(" %d", Key[i]);
  }

  printf("\n");
  
  return 0;
}

void build(){
  int i;

  for(i = n/2; i >= 1; i--){
    Max_h(i);
  }
}

void Max_h(int i){
  int left, right, largest = 0;
  int temp;

  left = i*2;
  right = i*2 + 1;

  if(left <= n && Key[left] > Key[i]) largest = left;
  else largest = i;

  if(right <= n && Key[right] > Key[largest]) largest = right;

  if(largest != i){
    temp = Key[i];
    Key[i] = Key[largest];
    Key[largest] = temp;

    Max_h(largest);
  }
}

