#include <stdio.h>
#define M 5000000

int heap[M];
int n;
void getmax(int);

void build(void);

int main(){
    int i;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&heap[i]);
    }
    build();
    for(i = 1;i <= n;i++){
        printf(" %d",heap[i]);
    }
    printf("\n");
    
    return 0;
    
}

void getmax(int i){
    int l=2*i;
    int r=2*i+1;
    int m,p;
    
    if(l<=n && heap[i]< heap[l]){
        m=l;
    }
    else {
        m=i;
    }
    if(heap[r] > heap[m] && r<=n){
        m=r;
    }
    if(m!=i){
        p=heap[i];
        heap[i]=heap[m];
        heap[m]=p;
        getmax(m);
    }
}

void build(void){
    int i;
    for(i=(n/2);i>=1;i--){
        getmax(i);
    }
}

