#include <stdio.h>

int Array[500000];
int H;

void maxHeapify(int i){
    int left = 2*(i+1)-1;
    int right = 2*(i+1);
    int largest;
    // select the node which has the maximum value
    if ( left <= H && Array[left] > Array[i])
        largest = left;
    else 
        largest = i;
    if ( right <= H && Array[right] > Array[largest])
        largest = right;

    if (largest != i)　// value of children is larger than that of i
    {
        int temp = Array[i];
        Array[i] = Array[largest];
        Array[largest] = temp;
        maxHeapify(largest); // call recursively
    }
}

//void buildMaxHeap(A)
//2    for i = H/2 downto 1
//3        maxHeapify(A, i)

int main(){
    scanf("%d\n",&H);
    for ( int ix = 0 ; ix < H ; ix++){
        if ( ix == H-1) scanf("%d\n",&Array[ix]);
        else scanf("%d ",&Array[ix]);
        
    for ( int ix = H/2 ; ix > 0 ; ix--)
        maxHeapify(ix);
    
    return 0;
}
