#include<stdio.h>
#define N 2000000
int n,A[N+1];
void maxHeapify(int);
int main(){
  int i;
  scanf("%d", &n);
  for(i = 1; i <= n; i++)
    scanf("%d", &A[i]);
  for(i = n/2; i >= 1; i--)
    maxHeapify(i);
  for(i = 1; i <= n; i++)
    printf(" %d", A[i]);
  printf("\n");

  return 0;
}

void maxHeapify(int i){
  int left,right,largest,t;
  left = 2*i;
  right = 2*i+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){
    t = A[i];
    A[i] = A[largest];
    A[largest] = t;
    maxHeapify(largest);
  }
}


