#include <stdio.h>
#define H 500000

void buildMaxHeap(int*,int);
void maxHeapify(int*,int, int);
int left(int);
int right(int);

int main(){
  int i, k, A[H];

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

  buildMaxHeap(A,k);

  for(i = 1; i <= k; i++)
    printf(" %d",A[i]);
  printf("\n");
  
  return 0;
}

void buildMaxHeap(int* A, int k){
  int i;
  for(i = k/2; i >= 1; i--)
    maxHeapify(A,i,k);
}

void maxHeapify(int* A, int i, int k){
  int l, r, largest, tmp;
  l = left(i);
  r = right(i);
  if(l <= k &&A[l] > A[i])
    largest = l;
  else largest = i;

  if(r <= k && A[r] > A[largest])
    largest = r;

  if(largest != i){
    tmp = A[i];
    A[i] = A[largest];
    A[largest] = tmp;
    maxHeapify(A, largest, k);
  }
}

int left(int i){
  return 2*i;
}

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