#include <stdio.h>

int N;
int T[500000] ={0};

void swap(int T[], int x, int y)
{
    int tmp = T[x];
    T[x] = T[y];
    T[y] = tmp;
}

void maxHeapify(int T[], int i)
{
    int parent = i;
    int left = (i+1)*2 - 1;
    int right = (i+1)*2;
    
    int largest;
    if (T[parent] < T[right] && T[left] < T[right]) {
        largest = right;
    }
    else if (T[parent] < T[left] && T[right] < T[left]) {
        largest = left;
    }
    else {
        largest = parent;
    }
    
    /*
    int j;
    printf("p:%d -- ", i);
    for (j = 0; j < 10; j++) {
        printf(" %d", T[j]);
    }puts("");
    */
    
    if (largest != parent) {
        swap(T, largest, parent);
        maxHeapify(T, largest);
    }    
    
    return;
}


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