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

#define SMALLEST_VALUE (-(2*1000*1000*1000+1))
#define MAX_ARRAY_SIZE (500 * 1000)

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

void max_heapify(int* array, int index)
{
    int left_index = index*2 + 1;
    int right_index = index*2 + 2;
    int largest_index;
    if(array[left_index]  <= array[index] &&
       array[right_index] <= array[index]) {
        largest_index = index;
    } else if(array[right_index] <= array[left_index] &&
              array[index]       <= array[left_index]) {
        largest_index = left_index;
    } else {
        largest_index = right_index;
    }

    if(largest_index != index) {
        swap(array + largest_index, array + index);
        max_heapify(array, largest_index);
    }
}

void build_max_heap(int* array, int size)
{
    for(int i = size / 2 - 1; 0 <= i; i--) {
        max_heapify(array, i);
    }
}

int main()
{
    int input_len;
    scanf("%d", &input_len);
    int* array = malloc(MAX_ARRAY_SIZE * sizeof(int));

    for(int i = 0; i < input_len; i++) {
        scanf("%d", &array[i]);
    }
    for(int i = input_len; i < MAX_ARRAY_SIZE; i++) {
        array[i] = SMALLEST_VALUE;
    }

    build_max_heap(array, input_len);

    for(int i = 0; i < input_len; i++) {
        printf(" %d", array[i]);
    }
    putchar('\n');

    free(array);
    return 0;
}