/*
* Complete Binaly Tree
* output 1)node_number, 2)parent_number, 3)depth, 4)kind_of_node, 5)child_list
*/

/* inclution */
#include <stdio.h>

/* definition */
#define HEAP_SIZE_MAX 250
#define VALUE_MAX 2000000
#define VALUE_MIN -2000000
#define NIL -1

/* Prototype */
int parent(int);
int left(int);
int right(int);
void maxHeapify(int);
void swap(int, int);


/* Global Variables */
int n, A[HEAP_SIZE_MAX+1];

/* ??????????????´?????§????§?????????? */
void maxHeapify(int i) {
  int l, r, largest;
  l = left(i);
  r = right(i);
  // ????????????????????????????????§???????????§????????????????????¶
  if(l<=n && A[l]>A[i]) { //?????????????????°???
    largest = l;
  }else {
    largest = i;
  }
  if(r<=n && A[r]>A[largest]) { //?¬????????????°??????????????¨?????????
    largest = r;
  }
  if(largest != i){ // i ???????????????????????§????????´???
    swap(i, largest);
    maxHeapify(largest); // ?????°??????????????????
  }
}

void swap(int i, int largest) {
  int tmp;  //A[i] ??¨ A[largest] ?????????;
    tmp = A[i];
    A[i] = A[largest];
    A[largest] = tmp;
}

/* node operation */
int parent(int i){
  return i/2;
}
int left(int i){
  return 2 * i;
}
int right(int i){
  return 2 * i + 1;
}

/* main */
int main(){
  int i;
  
  /* input the number of A[] */
  scanf("%d",&n);
  
  /* input array */
  for(i=1;i<=n;i++) {
    scanf("%d", &A[i]);
  }

  for(i=n/2;i>=1;i--){ // O(logn)
    maxHeapify(i);
  }

  /* print */
  for(i=1;i<=n;i++) {
    printf(" %d", A[i]);
  }

  return 0;
}