#include <stdio.h>

void max_heapify(int *heap, int i, int heap_size) {
	int L, R, largest, tmp;

	L = 2*i;
	R = 2*i + 1;
	if (L <= heap_size && heap[L] > heap[i]) {
		largest = L;
	} else {
		largest = i;
	}
	if (R <= heap_size && heap[R] > heap[largest]) {
		largest = R;
	}
	if (largest != i) {
		tmp = heap[i];
		heap[i] = heap[largest];
		heap[largest] = tmp;
		max_heapify(heap, largest, heap_size);
	}
}

void build_max_heap(int *heap, int heap_size) {
	int i;

	for (i = heap_size/2; i > 0; i--) {
		max_heapify(heap, i, heap_size);
	}
}

int main(void) {
	int heap[500001], heap_size, i;

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

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