#include <stdio.h>
#define N 500000

int heap[N], H;

void swap(int *a, int *b) {
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

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

int left(int id) {
    return 2 * id;
}

int parent(int id) {
    return id / 2;
}

void maxHeapify(int id) {
    int largest = id;

    if (left(id) <= H && heap[left(id)] > heap[largest]) {
        largest = left(id);
    }
    if (right(id) <= H && heap[right(id)] > heap[largest]) {
        largest = right(id);
    }

    if (largest != id) {
        swap(&heap[largest], &heap[id]);
        maxHeapify(largest);
    }
}

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

int main() {
    int i;

    scanf("%d", &H);

    for (i = 1; i <= H; i++) {
        scanf("%d", &heap[i]);
    }

    buildMaxHeap();

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