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

#define N 500000

int parent(int);
int right(int);
int left(int);
void max_heapify(int);
void build_max_heap(void); 

int A[N], n;

int main(){
  int i, key;

  scanf("%d", &n);

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

  build_max_heap();
  
  for(i = 1; i <= n; i++){
    printf(" %d", A[i]);
  }

  printf("\n");

  return 0;
}

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

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

int left(int i){
  return i * 2;
}
    
void max_heapify(int i){
  int l, r, biggest, tmp;

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

  if(l <= n && A[l] > A[i])
    biggest = l;

  else
    biggest = i;

  if(r <= n && A[r] > A[biggest])
    biggest = r;

  if(biggest != i){
    tmp = A[i];

    A[i] = A[biggest];
    A[biggest] = tmp;

    max_heapify(biggest);
  }
}

void build_max_heap(){
  int i;

  for(i = n / 2; i > 0; i--)
    max_heapify(i);

  return;
}
    

