#include <stdio.h>

#define M 500000     

#define right(i) (i*2)      
#define left(i)  (i*2+1)    

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

int main(){
    int a[M+1];
    int h;
    int j;
    scanf("%d",&h);
    for( j= 1; j<= h; j++ ){
        scanf("%d",&(a[j]));
    }
    for(j=h/2;j>0;j--){maxHeapify(h,a,j);}

    for( j= 1; j<= h; j++ ){
        printf(" %d",a[j]);
    }
    printf("\n");

    return 0;
}



