#include <stdio.h>
#define HEAP 500000

void maxHeapify(int *, int);
void buildMaxHeap(int *);
int n;

int main(){
  int i, key[HEAP];

  scanf("%d", &n);

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

  buildMaxHeap(key);

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

  return 0;
}

void maxHeapify(int *A, int i){
  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;
    maxHeapify(A, largest);
  }
}

void buildMaxHeap(int *A){
  int i;

  for(i = n / 2; i >= 1; i--)
    maxHeapify(A, i);
}