#include <stdio.h>
#define M 500001
#define NIL -1

void maxHeapify(int);
void buildMaxHeap(void);
int A[M],limit;
int left(int );
int right (int );

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

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

void maxHeapify(int i){
    int l,r,largest,ch;
    l=left(i);
    r = right(i);
    if(l<=limit && A[l]>A[i]){
        largest = l;
    }
    else largest = i;
    if(r <= limit && A[r] > A[largest]){
        largest = r;
    }
    if(largest != i){
        ch = A[i];
        A[i] = A[largest];
        A[largest] = ch;
        maxHeapify(largest);
    }
}

int left(int i){
    return 2 * i;
}

int right (int i){
    return 2 * i + 1;
}


