#include<stdio.h>
#include<stdlib.h>

#define MAX 500001

void MaxHeap(int*, int, int);

int main(void){
  int i, n;
  int A[MAX];

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

  for(i=n/2; i>0; i--) MaxHeap(A, i, n);

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

  return 0;
}

void MaxHeap(int *A, int i, int n){
  int left, right, largest, tmp;

  left = i*2;
  right = i*2+1;

  if(left <= n && A[left] > A[i]) largest = left;
  else largest = i;

  if(right <= n && A[right] > A[largest]) largest = right;

  if(largest != i){
    tmp = A[i];
    A[i] = A[largest];
    A[largest] = tmp;
    MaxHeap(A, largest, n);
  }

  return;
}