#include <stdio.h>

#define MAX_H 500000

int H;
int A[MAX_H];

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

void maxHeapify(int idx) {
  int l = 2*idx+1;
  int r = 2*idx+2;
  int largest = idx;
  if(l < H && A[largest] < A[l]) largest = l;
  if(r < H && A[largest] < A[r]) largest = r;
  
  if(largest != idx) {
    swap(A+idx, A+largest);
    maxHeapify(largest);
  }
}

void buildMaxHeap() {
  int i;
  for(i = H/2; i >= 0; i--) maxHeapify(i);
}

int main() {
  int i;
  scanf("%d", &H);  
  for(i = 0; i < H; i++) scanf("%d", A+i);

  buildMaxHeap();

  for(i = 0; i < H; i++) printf(" %d", A[i]);
  puts("");
  
  return 0;
}