#include <stdio.h>

#define H 500000

void maxHeapify(int);
void swap(int*, 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 swap(int* a, int* b){
  int tmp;
  tmp = *a;
  *a = *b;
  *b = tmp;
}
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){
    swap(&Heap[max], &Heap[i]);
    maxHeapify(max);
  }
}