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

#define N 4000000

void buildmaxheap(int *);
void maxHeapify(int *,int);

int left(int);
int right(int);
int n;

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

void buildmaxheap(int *A){
    int i;
    
    for(i = n / 2;i >= 1;i--){
        maxHeapify(A,i);
    }
}

void maxHeapify(int *A,int i){
    int l,r,temp,largest;
    
    l = left(i);
    r = right(i);
    
    if((l <= n) && (A[l] > A[i])){
        largest = l;
    }
    else{
        largest = i;
    }
    if((r <= n) && (A[r] > A[largest])){
        largest = r;
    }
    
    if(largest != i){
        temp = A[i];
        A[i] = A[largest];
        A[largest] = temp;
        maxHeapify(A,largest);
    }
}

int left(int id){
    int lid;
    
    lid = 2 * id;
    
    return lid;
}

int right(int id){
    int rid;
    
    rid = 2 * id + 1;
    
    return rid;
}
