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

void s(int *, int *);
void maxheap(int*, int, int);
void buildmaxheap(int*, int);

main(){
????int h, i, *A;

????scanf("%d", &h);
????A = malloc(sizeof(int)*(h+1));
????for(i=1; i<=h; i++){
????????scanf("%d", A+i);
????}

????buildmaxheap(A, h);
????for(i=1; i<=h; i++){
????????printf(" %d", *(A+i));
????}
????printf("\n");

????free(A);
????return 0;
}

void s(int *a, int *b){
????int tmp;

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

void maxheap(int *A, int h, int i){
????int largest = i;
????int l = i*2;
????int r = i*2 + 1;

????if(l<=h && A[l] > A[i]) largest = l;
????if(r<=h && A[r] > A[largest]) largest = r;

????if(largest != i){
????????s(A+largest, A+i);
????????maxheap(A, h, largest);
????}
}