#include <stdio.h>

int H[500005],n;

void max(int x)
{
    int l,r,largest,d;
    l=2*x;
    r=l+1;

    if( l <= n && H[l] > H[x] ) 
        largest = l;
    else
        largest = x;
    if( r <= n && H[r] > H[largest] )
        largest = r;
   
    if( largest != x ) {
        d=H[largest];
        H[largest]=H[x];
        H[x]=d;
        max(largest);   
    }
}

int main()
{ 
    int i;

    scanf("%d",&n);

    for(i=1;i<=n;i++){
        scanf("%d",&H[i]);
    } 

    for(i=n/2;i>=1;i--){
        max(i);
    }

    for(i =1;i<=n;i++){
        printf(" %d",H[i]);
    }

    printf("\n");

    return 0;
}
