/* Maximum Heap */
#include <stdio.h>

int A[500005], i, n;

void maxHeapify(int p)
{
    int left = p * 2 + 1, right = p * 2 + 2, large = p; 
    
    if(left <= n && A[left] > A[large]){
        large = left;
    }
    if(right <= n && A[right] > A[large]){
        large = right;
    }
    if(large != p){
        right = A[p];
        A[p] = A[large];
        A[large] = right;
        maxHeapify(large);
    }
}

int main()
{
    scanf("%d", &n);
    for(i=0; i<n; i++){
        scanf("%d", A+i);
    }
    for(i=n/2; i>=0; i--){
        maxHeapify(i);
    }
    for(i=0; i<n; i++){
        printf(" %d", A[i]);
    }
    puts("");
    return 0;
}
