#include <stdio.h>

#define MAX 50000000

int parent(int i) {return i / 2;}
int left(int i) {return 2*i;}
int right(int i){return 2*i + 1;}
int H;
int A[MAX];

void maxHeap(int idx){
    int l = left(idx);
    int r = right(idx);
    int large;
    if(l <= H && A[idx] < A[l]){
        large = l;
    } else {
        large = idx;
    }

    if(r <= H && A[large] < A[r]){
        large = r;
    }

    if(large != idx){
        int tmp = A[idx];
        A[idx] = A[large];
        A[large] = tmp;
        maxHeap(large);
    }

}

void exec(){
    for(int i = H/2; i >= 1; i--) maxHeap(i);
}

int main(){
    int i;
    scanf("%d", &H);
    for(i = 1; i <= H; i++){
        scanf("%d", &A[i]);
    }

    exec();

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

    return 0;
}

