#include <stdio.h>
#define N 500002

void BuildMaxHeap(int *);
void MaxHeapify(int *, int);

int h;

int main(){
  int a[N];
  int i;

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

  BuildMaxHeap(a);

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

  return 0;
}

void BuildMaxHeap(int *a){
  int i;

  for(i = h / 2; i > 0; i--) MaxHeapify(a, i);
}

void MaxHeapify(int *a, int i){
  int left, right, large, change;

  if(i * 2 <= h) left = i * 2;
  else left = i;
  if(i * 2 + 1 <= h) right = i * 2 + 1;
  else right = i;


  if(left < h + 1 && a[left] > a[i]) large = left;
  else large = i;
  if(right < h + 1 && a[right] > a[large]) large = right;
  
  if(large != i){
    change = a[i];
    a[i] = a[large];
    a[large] = change;
    MaxHeapify(a, large);
  }
}