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

void buildMaxHeap(int* arr, size_t size);
void heapMaxify(int* arr, int root, size_t size);
void swap(int* a, int* b);

void printHeap(int* arr, size_t size) {
	for (int i = 1; i <= size; i++) {
		printf(" %d", arr[i]);
		if (i == size)
			printf("\n");
	}
}

int main() {
	unsigned int m, * H;

	scanf("%d", &m);

	H = (int*)malloc((m + 1) * sizeof(int));

	for (int i = 1; i <= m; i++)
		scanf("%d", H + i);

	buildMaxHeap(H, m);
	printHeap(H, m);

	return 0;
}

void buildMaxHeap(int* arr, size_t size) {
	for (int i = floor(size / 2); i > 0; i--)
		heapMaxify(arr, i, size);
}
void heapMaxify(int* arr, int root, size_t size) {
	int left = 2 * root;
	int right = 2 * root + 1;
	int largest = root;

	largest = left <= size && arr[left] > arr[largest] ? left : largest;
	largest = right <= size && arr[right] > arr[largest] ? right : largest;

	if (root != largest) {
		swap(arr + root, arr + largest);
		heapMaxify(arr, largest, size);
	}
}
void swap(int* a, int* b) {
	int temp = *a;
	*a = *b;
	*b = temp;
}
