#include <stdio.h>
#include <stdlib.h>

void heap(int a[],int i,int n){
    int l,r,max,k;
    if(i*2+1>=n){
        return;
    }else if(i*2+2==n){
        if(a[i]<a[n-1]){
            r=a[i];
            a[i]=a[n-1];
            a[n-1]=r;
        }
        return;
    }else{
        l = i*2+1;
        r = i*2+2;
        max = i;
        if(a[i]<a[l]){
            max = l;
        }
        if(a[max]<a[r]){
            max = r;
        }
        if(max!=i){
            k=a[i];
            a[i]=a[max];
            a[max]=k;
            heap(a,max,n);
        }
    }
}

int main(void){
    int n,i,j,k;
    int *a;
    scanf("%d",&n);
    a = (int*)malloc(sizeof(int)*n);
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    for(i=n/2;i>=0;i--){
        heap(a,i,n);
    }
    for(i=0;i<n;i++){
        printf(" %d",a[i]);
    }
    printf("\n");
    free(a);
    return 0;
}
