#include <stdio.h>
#include <stdlib.h>
#define H 500001

int left(int i){ return i*2; }

int right(int i){ return i*2 + 1; }

int parent(int i){ return i/2; }

int n;

void maxHeapify(int *arr, int i){
  int largest,tmp;
  int l = left(i), r = right(i);

  // choice largest key in this and parents
  if(i <= n && arr[l] > arr[i]) largest = l;
  else largest = i;
  if(r <= n && arr[r] > arr[largest]) largest = r;

  if(largest != i){ // if arr[i]'s parent key larger than arr[i]'s key
    tmp = arr[i];
    arr[i] = arr[largest];
    arr[largest] = tmp;
    maxHeapify(arr, largest);
  }
}

void buildMaxHeap(int *arr){
  int i;
  for(i=n/2; i>=1; i--) maxHeapify(arr, i);
}

int main(void){
  int i;
  int heap[H];

  // input and alloc
  scanf("%d", &n);
  for(i=1; i<=n; i++) scanf("%d", &heap[i]);

  // build heap
  buildMaxHeap(heap);

  // output
  for(i=1; i<=n; i++) printf(" %d", heap[i]);
  printf("\n");

  return 0;
}