#include<stdio.h>
#define MAX 500000

int heap[MAX];

void swap(int i,int j){
  int buf = heap[i];
  
  heap[i] = heap[j];
  heap[j] = buf;
}

void maxHeap(int H, int i) {
  int l = 2 * i, r = 2 * i + 1;
  int largest = i;
  
  if (l <= H && heap[l] > heap[i])largest = l;
  if (r <= H && heap[r] > heap[largest])largest = r;
  if (largest != i) {
    swap(i, largest);
    maxHeap(H, largest);
  }
}

void buildMaxHeap(int H) {
  for (int i = H / 2; i >= 1; i--) {
    maxHeap(H, i);
  }
}

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

