#include<stdio.h>
#include<stdlib.h>

int n;
int *N;

void maxHeapify(int i){
  int l, r, largest, a;

  l = 2 * i;
  r = 2 * i + 1;

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

  else largest = i;

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

  if(largest != i){
    a = N[i];
    N[i] = N[largest];
    N[largest] = a;
    maxHeapify(largest);
  }
}

void buildMaxHeap(){
  int i;

  for(i = n / 2; i >= 1; i--) maxHeapify(i);
}

int main(){
  int i;

  scanf("%d", &n);

  N = (int *)malloc(sizeof(int) * (n + 1));

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

  buildMaxHeap();

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

  printf("\n");

  return 0;
}