#include <stdio.h>
#define exchange(a,b) do{__typeof(a) __t; __t=a; a=b; b=__t;}while(0)
#define parent(i) (i/2)
#define left(i) (i*2)
#define right(i) (i*2+1)
#define MAX_N 500001

int N;
int heap[MAX_N];

void maxHeapify(int A[], int i) {
  int l = left(i);
  int r = right(i);
  int largest;
  if(l <= N && A[l] > A[i]) {
    largest = l;
  } else {
    largest = i;
  }
  if(r <= N && A[r] > A[largest]) {
    largest = r;
  }
  if(largest != i) {
    exchange(A[i], A[largest]);
    maxHeapify(A, largest);
  }
}

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

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