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

#define MAX_NUM (500000)

static void MaxHeapify(int heap[], int size, int index) {
    int l = index * 2;
    int r = index * 2 + 1;
    int largest;

    if ( l <= size && heap[l] > heap[index] ) {
        largest = l;
    } else {
        largest = index;
    }
    if ( r <= size && heap[r] > heap[largest] ) {
        largest = r;
    }

    if ( largest != index ) {
        int t;

        t = heap[index];
        heap[index] = heap[largest];
        heap[largest] = t;

        MaxHeapify(heap, size, largest);
    }

    return;
}

static void BuildMaxHeap(int heap[], int size) {
    int i;
    for ( i = size / 2; i > 0; i-- ) {
        MaxHeapify(heap, size, i);
    }

    return;
}

int main(int argc, char* argv[]) {
    int n;
    int i;
    int heap[MAX_NUM + 1];

    scanf("%d\n", &n);
    for ( i = 1; i <= n; i++ ) {
        int d;
        scanf("%d", &d);
        heap[i] = d;
    }

    BuildMaxHeap(heap, n);

    for ( i = 1; i <= n; i++ ) {
        printf(" %d", heap[i]);
    }
    printf("\n");

    return 0;
}

