#include <stdio.h>

#define H 500000

int main(){
  int Heap[H], i, n;
  int max, left, right, reserve;

  scanf("%d", &n);

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

  for(i = n/2; i > 0; i--){
    left = i*2;
    right = i*2+1;

    if(left <= n && Heap[i*2] > Heap[i])
      max = left;
    else
      max = i;
    if(right < n && Heap[i*2+1] > Heap[max])
      max = right;

    if(max != i){
      i = reserve;
      i = max;
      max = reserve;
    }
  }

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

  printf("\n");

  return 0;
}