#include <stdio.h>

int A[5000001];
int H;

int left(int i)
{
    return i * 2;
}

int right(int i)
{
    return i * 2 + 1;
}

void maxHeapify(int A[], int i)
{
    int l, r;
    int largest;
    l = left(i);
    r = right(i);

    if(l <= H && A[l] > A[i])
        largest = l;
    else
        largest = i;

    if(r <= H && A[r] > A[largest])
        largest = r;

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

void buildMaxHeap(int A[])
{
    int i;
    for(i = H / 2; i > 0; i--)
        maxHeapify(A, i);
}

int main(void)
{
    int i;

    scanf("%d", &H);

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

    buildMaxHeap(A);

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

    printf("\n");

    return 0;
}