#include <stdio.h>
#define N 500001
#define NIL 500009

int n;

void buildMaxHeap(int A[]){
  int i;
  for(i = n/2; i > 0; i--){
   maxHeapify(A, i);
  }
}

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

  l = getLeft(i);
  r = getRight(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){
    tmp = A[i];
    A[i] = A[largest];
    A[largest] = tmp;
  }
  maxHeapify(A, largest);
}

int getLeft(int i){
  i *= 2;
  if(i > n) return NIL;
  else return i;
}

int getRight(int i){
  i = i * 2 + 1;
  if(i > n) return NIL;
  else return i;
}

main(){
  int i, heap[N];

  scanf("%d", &n);

  for(i = 1; i <= n; i++){
    scanf("%d", &heap[i]);
  }

  buildMaxHeap(heap);
  
  for(i = 1; i <= n ; i++){
    printf(" %d", heap[i]);
  }
  printf("\n");
  return 0;
}