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

void maxHeapify(int *A, int h, int i) {
	int left, right, largest, tmp;
	left = i * 2;
	right = left + 1;

	if (left <= h && A[left] > A[i]) largest = left;
	else largest = i;

	if (right <= h && A[right] > A[largest]) largest = right;

	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);
}

int main() {

	int h, i;
	int *A;

	scanf("%d", &h);
	A = (int*)malloc(sizeof(int) * (h + 1));
	for (i = 0; i < h; i++) {
		scanf("%d", &A[i + 1]);
	}

	buildMaxHeap(A, h);

	for (i = 1; i <= h; i++) {
		printf(" %d", A[i]);
	}

	printf("\n");

	return 0;
}
