#include <stdio.h>

#define NODE_MAX (2000000)

struct node {
        int key;
        int parent;
        int left;
        int right;
};

void
max_heapify(struct node A[], int i, int heap_size)
{
        int largest = i;
        int left = A[i].left;
        int right = A[i].right;
        int key;

        if (A[left].key <= A[i].key && A[right].key <= A[i].key)
                largest = i;
        else if (right <= heap_size && A[left].key <= A[right].key)
                largest = right;
        else if (left <= heap_size)
                largest = left;

        if (largest != i) {
                key = A[i].key;
                A[i].key = A[largest].key;
                A[largest].key = key;
                max_heapify(A, largest, heap_size);
        }

}

int
main(void)
{
        int heap_size;
        struct node key[NODE_MAX];
        int i;

        scanf("%d", &heap_size);

        for (i = 1; i <= heap_size; i++)
                scanf("%d", &key[i].key);

        for (i = 1; i <= heap_size; i++) {
                key[i].parent = i / 2;
                key[i].left = i * 2;
                key[i].right = i * 2 + 1;
        }

        for (i = heap_size; 0 < i; i--)
                max_heapify(key, i, heap_size);

        for (i = 1; i <= heap_size; i++)
                printf(" %d", key[i].key);
        printf("\n");

        return 0;
}

