#include <stdio.h>

#define H 500000

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

  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[left] > Heap[i]))
      max = left;
    else
      max = i;

    if(right < n && (Heap[right] > Heap[max]))
      max = right;

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

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

  printf("\n");

return 0;
}