//
// Created by kota_ on 2020/01/06.
//
#include <stdio.h>
#include <stdlib.h>

void maxHeapify(int *A, int H, int i);
int left(int i);
int right(int i);
void buildMaxHeap(int *A, int H);

int main(){
    int i, H, *A;
    scanf("%d", &H);
    A = malloc(sizeof(int) * H);
    for(i = 0; i < H; i++){
        scanf("%d", &A[i]);
    }
    buildMaxHeap(A, H);
    for(i = 0; i < H; i++){
        printf(" %d", A[i]);
    }
    printf("\n");
    free(A);
    return 0;
}

int left(int i){
    return 2 * i + 1;
}

int right(int i){
    return 2 * (i + 1) ;
}

void maxHeapify(int *A, int H, int i){
    int l, r, largest, tmp;
    l = left(i);
    r = right(i);
    if(l < H && A[l] > A[i]){
        largest = l;
    }
    else{
        largest = i;
    }
    if(r < H && A[r] > A[largest]){
        largest = r;
    }
    if(largest != i){
        tmp = A[i];
        A[i] = A[largest];
        A[largest] = tmp;
        maxHeapify(A, H, largest);
    }
}


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

