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

void maxHeapify(int*, int, int);
void buildMaxHeap(int*, int);
void swap(int *, int *);

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

  scanf("%d", &h);
  A = malloc(sizeof(int)*(h+1));
  for(i=1; i<=h; i++){
    scanf("%d", A+i);
  }

  buildMaxHeap(A, h);
  for(i=1; i<=h; i++){
    printf(" %d", *(A+i));
  }
  printf("\n");

  free(A);
  return 0;
}

void maxHeapify(int *A, int h, int i){
  int largest = i;
  int l = i*2;
  int r = i*2 + 1;

  if(l<=h && A[l] > A[i]) largest = l;
  if(r<=h && A[r] > A[largest]) largest = r;

  if(largest != i){
    swap(A+largest, A+i);
    maxHeapify(A, h, largest);
  }
}

void buildMaxHeap(int *A, int h){
  int i;

  for(i=h/2; i >= 1; i--){
    maxHeapify(A, h, i);
  }
}

void swap(int *a, int *b){
  int tmp;

  tmp = *a;
  *a = *b;
  *b = tmp;
}