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

int k;
int *A;

void maxHeap(int);
void construct();
void up(int *, int *);

int main(){
  int i;

  scanf("%d",&k);

  A = (int *)malloc((k+1) * sizeof(int));

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

  construct();

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

  printf("\n");

  free(A);
  return 0;
}

void maxHeap(int i){
  int l = 2*i;
  int r = 2*i + 1;
  int largest;

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

  else
    largest = i;

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

  if(largest != i){
    up(&A[i], &A[largest]);
    maxHeap(largest);
  }
}

void construct(){
  int i;

  for(i=k/2; i>0; i--)
    maxHeap(i);
}

void up(int *a, int *b){
  int tmp;

  tmp = *a;
  *a = *b;
  *b = tmp;
}



