#include <stdio.h>
#define N 100000000

int Heap,A[N];

void maxHeapify(int i){
  int left,right,L,tmp;
  left = 2 * i;
  right = 2 * i * 1;

  if(left <= Heap && A[left] > A[i]){
    L = left;
  }
  else {
    L = i;
  }
  if(right <= Heap && A[right] > A[L]){
    L = right;
  }

  if(L != i){
    tmp = A[i];
    A[i] = A[L];
    A[L] = tmp;
    maxHeapify(L);
  }
}

int main(){
  int i;
  scanf("%d",&Heap);
  for(i=1; i<=Heap; i++){
    scanf("%d",&A[i]);
  }
  for(i=Heap; i>=1; i--){
    maxHeapify(i);
  }
  for(i=1; i<=Heap; i++){
    printf(" %d",A[i]);
  }
  printf("\n");

  return 0;
}

