#include <stdio.h>

#define MAX_HEAP_SIZE   500001

int HeapSize;
int Heap[MAX_HEAP_SIZE];

void initVer(void){
    int i;
    
    for( i = 0; i < MAX_HEAP_SIZE; i++ ){
        Heap[i] = 0;
    }
}

void inputVal(void){
    int i;
    
    scanf("%d",&HeapSize);
    for( i = 1; i <= HeapSize; i++ ){
        scanf("%d",&Heap[i]);
    }
}

void maxHeapify(int Node){
    int LeftNode;
    int RightNode;
    int LargetNode;
    int TmpHeapVal;
    
    LeftNode = 2 * Node;
    RightNode = LeftNode + 1;
    
    if( ( LeftNode <= HeapSize ) && ( Heap[LeftNode] > Heap[Node] ) ){
        LargetNode = LeftNode;
    }else{
        LargetNode = Node;
    }
    
    if( ( RightNode <= HeapSize ) && ( Heap[RightNode] > Heap[LargetNode] ) ){
        LargetNode = RightNode;
    }
    
    if( LargetNode != Node ){
        TmpHeapVal = Heap[Node];
        Heap[Node] = Heap[LargetNode];
        Heap[LargetNode] = TmpHeapVal;
        maxHeapify(LargetNode);
    }
}

void buildMaxHeap(void){
    int i;
    
    for( i = HeapSize / 2; i > 0; i-- ){
        maxHeapify(i);
    }
}

void outputVal(void){
    int i;
    
    for( i = 1; i <= HeapSize; i++ ){
        printf("%d ",Heap[i]);
    }
}

int main(void){
    initVer();
    inputVal();
    buildMaxHeap();
    outputVal();
}


