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


void maxHeapify(int *A, int N, int node_idx){
    
    int tmp;
    int largest = node_idx;
    int left_child_idx = 2 * node_idx + 1;
    int right_child_idx = 2 * node_idx + 2;
    
    // ????????????????????????????????§???????????§????????????????????¶
    if ( left_child_idx < N && A[left_child_idx] > A[node_idx]) {
        largest = left_child_idx;
    }else{
        largest = node_idx;
    }
    if ( right_child_idx < N && A[right_child_idx] > A[largest]) {
        largest = right_child_idx;
    }
    
    // ???node_idx?????????????????????????????§????????´???
    if (largest != node_idx) {
        tmp = A[largest];
        A[largest] = A[node_idx];
        A[node_idx] = tmp;
        
        // ?????°??????????????????
        maxHeapify(A, N, largest);
    }
}


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


int main( ){
    
    int i, n;
    
    scanf("%d", &n);
    int A[n];
    
    for (i=0; i<n; i++) {
        scanf("%d", &A[i]);
    }
    
    buildMaxHeap(A, n);
    
    for (i=0; i<n; i++) {
        printf("%d", A[i]);
        if (i != n-1) {
            printf(" ");
        }
    }
    printf("\n");
    
    return 0;
}