#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n, *A;
void maxHeapify(int i){
  int l, r, max, cp;
  l = 2*i;
  r = 2*i+1;
  if(l <= n && A[l] > A[i])
    max = l;
  else max = i;
  if(r <= n && A[r] > A[max])
    max = r;
  if(max != i){
    cp = A[i];
    A[i] = A[max];
    A[max] = cp;
    maxHeapify(max);
  }
  return;
}
void buildMaxHeap(){
  int i;
  for(i = n/2; i > 0; i--)
    maxHeapify(i);
}
int main(){
  int i;
  scanf("%d",&n);
  A = malloc(sizeof(int)*(n+1));
  for(i = 1; i <= n; i++) scanf("%d",&A[i]);
  buildMaxHeap();
  for(i = 1; i <= n; i++)
    printf(" %d",A[i]);
  printf("\n");
  free(A);
  return 0;
}