#include <stdio.h>
#define MAX 50000

void buildMaxHeap(void);
void maxHeapify(int i);

int n;
int heap[MAX];

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

int Parent(int i){
    return i/2;
}
int Left(int i){
    return 2*i;
}
int Right(int i){
    return 2*i+1;
}

void maxHeapify(int i){
    int L,R,largest,temp;
    L=Left(i);
    R=Right(i);
    
    if(L<=n && heap[L]>heap[i]){
        largest=L;
    }
    else {
        largest=i;
    }
    if(R<=n && heap[R]>heap[largest]){
        largest=R;
    }
    
    if(largest!=i){
        temp=heap[i];
        heap[i]=heap[largest];
        heap[largest]=temp;
        maxHeapify(largest);
    }
}
void buildMaxHeap(void){
    int i;
    for(i=(int)n/2;i>0;i--){
        maxHeapify(i);
    }
}