#include <stdio.h>
#define MAX_HEAP 600000

int heap_size;
/* 木の移動 */
int parent(int);
int left(int);
int right(int);

void maxHeapfy(int *, int);
void buildMaxHeap(int *);

int main(void) {
	int i, n;
	int ary[MAX_HEAP];

	scanf("%d", &n);
	heap_size = n;
	for (i = 1; i <= n; i++) {
		scanf("%d", &ary[i]);
	}
	buildMaxHeap(ary);

	for (i = 1; i <= n; i++) {
		printf(" %d", ary[i]);
	}
	putchar('\n');

	return 0;
}

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

void maxHeapify(int *A, int i) {
	int l = left(i);
	int r = right(i);
	int largest;

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

	if (largest != i) {
		/* A[i]とA[largest]の値を交換 */
		int tmp = A[largest];
		A[largest] = A[i];
		A[i] = tmp;

		maxHeapify(A, largest);
	}
}

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