#include<stdio.h>
#define heap_max 500000
int inHeap;
long heap[heap_max];

void maxHeapify(long A[],int i){
    int l,r,largest;
    long temp;
    l = i*2;
    r = i*2+1;
    if(inHeap>=1 && A[l]>A[i]) largest = l;
    else largest = i;
    if(inHeap >= r && A[r] > A[largest]) largest = r;
    if(largest != i){
            temp = A[i];
            A[i] = A[largest];
            A[largest] = temp;
            maxHeapify(A,largest);
        }
    }
     
void buildMaxHeap(long A[]){
    int i;
    for(i=inHeap/2;i>=1;i--){
        maxHeapify(A,i);
        }
    }

	
int main(void){
    int i;
    
    scanf("%d",&inHeap);
    for(i=1;i<inHeap+1;i++) scanf("%ld",&heap[i]);
    buildMaxHeap(heap);
    for(i=1;i<inHeap+1;i++) printf(" %ld",heap[i]);
    printf("\n");
    return 0;
    }