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

int H;
int arr[500002];
int tmp = 0;

void maxHeapify(int i){
    int l = 2*i;
    int r = 2*i+1;
    int largest;
    if(l <= H && arr[l] > arr[i]){
        largest = l;
    }
    else largest = i;
    if(r <= H && arr[r] > arr[largest]){
        largest = r;
    }
    if(largest != i){
        tmp = arr[i];
        arr[i] = arr[largest];
        arr[largest] = tmp;
        maxHeapify(largest);
    }
}

void buildMaxHeap(){
    int i;
    for(i=H/2; i>0; i--){
        maxHeapify(i);
    }
}

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

    return 0;
}

