//Maximum Heap
//2019-01-15
//Ebitani Akira

#include <stdio.h>
#include <stdlib.h>

#define N 500001
void MaxHeapify(int);
void BuildMaxHeap();

int n;
int array[N];

int main(){
  int i;

  scanf("%d", &n);
  if(n < 1 || n > 500000){
    printf("ヒープサイズが適当ではありません。\n");
    exit(1);
  }

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

  BuildMaxHeap();

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

  printf("\n");

  return 0;
}

void BuildMaxHeap(){
  int i;
  for(i = n/2 ; i >= 1 ; i--){
    MaxHeapify(i);
  }
}

void MaxHeapify(int x){
  int left, right;
  int largest;

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

  if(left <= n && array[left] > array[x]){
    largest = left;
  }
  else{
    largest = x;
  }

  if(right <= n && array[right] > array[largest]){
    largest = right;
  }

  int temp;
  if(largest != x){
    temp = array[x];
    array[x] = array[largest];
    array[largest] = temp;
    MaxHeapify(largest);
  }

}

