#include<stdio.h>
#define MAX 500001
int heap[MAX];
int main() {
	int i, H;
	scanf("%d", &H);
	for (i = 1; i <= H; i++) {
		scanf("%d", &heap[i]);
	}
	buildMaxHeap(heap, H);
	for (i = 1; i <= H; i++) {
		printf(" %d", heap[i]);
	}
	printf("\n");
	return 0;
}

maxHeapify(int*A, int i,int H) {
	int l, r,largest,dum;
	l = 2 * i;
	r = 2 * i + 1;

	if (l < H&&A[l]>A[i])largest = l;
	else largest = i;
	if (r <= H&&A[r] > A[largest])largest = r;

	if (largest != i) {
		dum = A[i];
		A[i] = A[largest];
		A[largest] = dum;
		maxHeapify(A, largest, H);
	}
}

buildMaxHeap(int *A, int H) {
	int i;
	for (i = H / 2; i > 1; i--) {
		maxHeapify(A, i, H);
	}
}

