#include<stdio.h>
#define NLI NULL

int H[500000];
int left(int i){
  return i * 2;
}

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

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

void maxHeapify(int H[], int i, int n){
  int l, r, largest, x;

  l = left(i);
  r = right(i);

  if(l <= n && H[l] > H[i])
    largest = l;
  else
    largest = i;

  if(r <= n && H[r] > H[largest])
    largest = r;

  if(largest != i){
    x = H[i];
    H[i] = H[largest];
    H[largest] = x;
    maxHeapify(H, largest, n);
  }
}

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

int main(){
  int n, i, k;

  scanf("%d", &n);

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

  buildMaxHeap(H, n);

  for(i = 1; i < n + 1; i++)
    printf(" %d",H[i]);

  printf("\n");

  return 0;
}

