#include <stdio.h>

#define H 500000

void maxHeapify(int);

int Heap[H], n;

int main(){
  int i;

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

  for(i = n/2; i > 0; i--){
    maxHeapify(i);
  }

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

  printf("\n");

  return 0;
}

void maxHeapify(int i){
  int left, right, max, reserve;

  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[i])
    max = right;

  if(max != i){
    reserve = Heap[i];
    Heap[i] = Heap[max];
    Heap[max] = reserve;
    maxHeapify(max);
  }
}