#include <stdio.h>


// I/O
int get_int() {
    int n = 0;
    int c = getchar_unlocked();
    if(c == 45) {
        c = getchar_unlocked();
        while(47 < c && c < 58) n = 10 * n + (c & 0xf), c = getchar_unlocked();
        return -n;
    } else if(c < 48 || 57 < c) return c;
    while(47 < c && c < 58) n = 10 * n + (c & 0xf), c = getchar_unlocked();
    return n;
}
void put_int(int n) {
    if(!n) {
        putchar_unlocked('0');
        return;
    }
    if(n < 0) n = -n, putchar_unlocked('-');
    char buf[11];
    int i = 0;
    while(n) buf[i++] = (char)(n % 10 + '0'), n /= 10;
    while(i--)putchar_unlocked(buf[i]);
}
void put_str(char *str) { while(*str) putchar_unlocked(*str++); }
//


// maximum heap
void _maxheap(int *A, const int i, const int N) {
    int l = i * 2;
    int r = i * 2 + 1;
    int max = i;
    if(l <= N && A[l] > A[max]) max = l;
    if(r <= N && A[r] > A[max]) max = r;
    if(max != i) {
        int tmp = A[i];
        A[i] = A[max];
        A[max] = tmp;
        _maxheap(A, max, N);
    }
}
void maxheap(int *A, const int N) {
    for(int i = N / 2; i > 0; --i) _maxheap(A, i, N);
}
//


int main(int argc, char **argv) {
    int h = get_int();
    int a[h + 1];
    for(int i = 1; i <= h; ++i) a[i] = get_int();
    maxheap(a, h);
    for(int i = 1; i <= h; ++i) putchar_unlocked(' '), put_int(a[i]);
    putchar_unlocked('\n');
    return 0;
}

