#include <stdio.h>

int n;
void swap(int *A, int k, int l){
  int tmp = A[k];
  A[k] = A[l];
  A[l] = tmp;
}
void maxHeapify(int *A, int i){
  int largest;
  int l = 2 * i + 1;
  int r = 2 * i + 2;
  if(l < n && A[i] < A[l])
    largest = l;
  else 
    largest = i;
  if(r < n && A[largest] < A[r])
    largest = r;
  if(largest != i){
    swap(A, largest, i);
    maxHeapify(A, largest);
  }
}
void buildMaxHeap(int *A){
  for(int i = (i / 2 - 1); i >= 0; --i){
    maxHeapify(A, i);
  }
}
void printHeap(int *A){
  for(int i = 0; i < n; ++i){
    printf(" %d", A[i]);
  }
  printf("\n");
}
int main(){
  scanf("%d", &n);
  int A[n];
  for(int i = 0; i < n; ++i){
    scanf("%d", &A[i]);
  }

  buildMaxHeap(A);
  printHeap(A);
  return 0;
}

