#include <stdio.h>
int H;
int a[1000000];
void maxheapfy(int i){
    int left = 2*i;
    int right = 2*i +1;
    int largest;
    if(a[i]<a[right] && right<=H) largest = right;
    else largest = i;
    if(a[left]>a[largest] && left<=H ) largest = left;
    
    if(largest != i){
        int tmp = a[largest];
        a[largest] = a[i];
        a[i] = tmp;
        maxheapfy(largest);
    }
    
    
}

void build_maxheap(int i){
    
    for(i=H/2;i>=1;i--){
        maxheapfy(i);
    }
}

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

