#include <stdio.h>
#include <stdlib.h>

void perfecttree(int,int,int *);
void swapping(int *,int *);

int main(){
  int H,i,*A;

  scanf("%d",&H);
  A = (int *)malloc(sizeof(int) * (1 + H));
  A[0] = 99;
  for(i = 1;i < H + 1;i++){
    scanf("%d",&A[i]);
  }

  for(i = H / 2;i > 0;i--) perfecttree(i,H,A);

  for(i = 1;i < H + 1;i++) printf(" %d",A[i]);
    printf("\n");
  
  free(A);
  return 0;
}

void perfecttree(int i,int H,int *A){
  int big,left,right,j;
 
  left = 2 * i;
  right = 2 * i + 1;
  
  if(left < H + 1 && A[left] > A[i]) big = left;
  else big = i;
  if(right < H + 1 && A[right] > A[big]) big = right;
  
  if(big != i){
    swapping(&A[i],&A[big]);
    perfecttree(big,H,A);
  }
}

void swapping(int *a,int *b){
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
}

