
#include <stdio.h>


long H;
long heap[500001];

long right(long i) { return 2 * i + 1; }
long left(long i) { return 2 * i; }
long parent(long i) { return i / 2 ;}

void maxHeapify(long i) {
	long r = right(i);
	long l = left(i);

	long max;

	if (r<=H && heap[r]>heap[i]) {
		max = r;
	}
	else {
		max = i;
	}

	if (l<=H && heap[l]>heap[max]) {
		max = l;
	}

	if (i != max) {
		long t = heap[i];
		heap[i] = heap[max];
		heap[max] = t;

		maxHeapify(max);
	}

}

int main() {
	scanf("%ld", &H);

	for (long i = 1; i <= H; ++i) {
		scanf("%ld", &heap[i]);
	}

	for (long i = H / 2; i > 0; i--) {
		maxHeapify(i);
	}

	for (long i = 1; i <= H; ++i) {
		printf(" %d", heap[i]);
	}
	printf("\n");
	return 0;
}
