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

void maxHeapfy(int *A, int i, int n) {
	int left, right, largest, tmp;

	left = i * 2;
	right = left + 1;
	if (left <= n && A[left] > A[i]) largest = left;
	else largest = i;
	if (right <= n && A[right] > A[largest]) largest = right;

	if (largest != i) {
		tmp = A[largest];
		A[largest] = A[i];
		A[i] = tmp;
		maxHeapfy(A, largest, n);
	}
}

void buildMaxHeap(int *A, int n) {
	int i;
	for (i = n / 2; i > 0; i--) {
		maxHeapfy(A, i, n);
	}
}

int main() {
	int i, k, n;
	int *A;

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

	buildMaxHeap(A, n);

	for (i = 1; i <= n; i++) {
		if (i > 1) printf(" ");
		printf("%d", A[i]);
	}
	printf("\n");

	free(A);

	return 0;
}
