#include <stdio.h>
#define M 500000
 
int heap[M+1];
int n;
 
void maxheap(int *,int);
void(int *);
 
int main(int argc, char *argv[])
{
    int i;
    scanf("%d", &n);
    for(i=1; i<=n; i++) {
        scanf("%d", heap+i);
    }
     
    bmaxheap(heap);
     
    for(i=1; i<=n; i++) {
        printf(" %d", heap[i]);
    }
    printf("\n");
    return 0;
}

void maxheap(int A[], int i)
{
    int tmp;
    int l=i+i;
    int r=l+1;
    int large=i;
 
    if(l<=n && A[l]>A[i])
        large=l;
    if(r<=n && A[r] > A[large])
        large=r;
     
    if(large!=i) {
        tmp=A[i]; A[i]=A[large]; A[large]=tmp;
            maxheap(A, large);
    }
 
}
 
void bmaxheap(int A[])
{
    int i;
    for(i=n/2; i>0; i--) {
        maxheap(A, i);
    }
}