#include <stdio.h>

int A[500000];
int num;

void maxHeapify(int A[],int i){
  int tmp,largest,l,r;

  l = i*2;
  r = i*2+1;

  if (l <= num && A[l] > A[i])largest = l;
  else  largest = i;
  if (r <= num && A[r] > A[largest]) largest = r;
  if (largest != i){
    tmp = A[i];
    A[i] = A[largest];
    A[largest] = tmp;
    maxHeapify(A,largest);
  }
}


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

}