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

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


int n;

int main(){
    int *heap;

    scanf("%d",&n);
    heap=malloc(sizeof(int)*n+1);
    for(int i=1;i<=n;i++){
        scanf("%d",&heap[i]);
    }

    buildmaxheap(heap);

    for(int i=1;i<=n;i++){
        printf(" %d",heap[i]);
    }

    printf("\n");

    return 0;
}

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

void maxheap(int* A,int i){
    int l=i*2;
    int r=i*2+1;
    int largest;
    int tmp;

    if(l<=n&&A[l]>A[i]){
        largest=l;
    }
    else{
        largest=i;
    }

    if(r<=n&&A[r]>A[largest]){
        largest=r;
    }

    if(largest!=i){
        tmp=A[i];
        A[i]=A[largest];
        A[largest]=tmp;
        maxheap(A,largest);
    }
}

