#include <stdio.h>

#define MAX 500001

static int H;
static int A[MAX];

static void maxHeapify(int i)
{
    int left = 2 * i;
    int right = 2 * i + 1;
    int largest;

    if (left <= H && A[left] > A[i]) {
        largest = left;
    } else {
        largest = i;
    }

    if (right <= H && A[right] > A[largest]) {
        largest = right;
    }

    if (largest != i) {
        int tmp = A[i];
        A[i] = A[largest];
        A[largest] = tmp;
        maxHeapify(largest);
    }
}

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

    for (int i = H / 2; i >= 1; i--) {
        maxHeapify(i);
    }

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

    return 0;
}
