#include<stdio.h>
#define N 1000000
int h;
void maxHeapify(int *,int);
void buildMaxHeap(int *);

int main(){
    int A[N],i;

    scanf("%d",&h);
    for(i = 1;i <= h;i++){
        scanf("%d",&A[i]);
    }
    buildMaxHeap(A);

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

    return 0;
}

void maxHeapify(int *A,int i){
    int a,l,r,max;
    l = i*2;
    r = i*2+1;

    if(l <= h && A[l] > A[i])max = l;
    else max = i;
    
    if(r <= h && A[r] >A[max])max = r;

    if(max != i){
        a = A[i];
        A[i] = A[max];
        A[max] = a;
        maxHeapify(A,max);
    }
}

void buildMaxHeap(int *A){
    int i;

    for(i = h/2;i >=1;i--)maxHeapify(A,i);
}
