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

#define SWAP(a, b, v) v = a, a = b, b = v
#define left(i) (2 * (i))
#define right(i) (2 * (i) + 1)

void
max_heapify(int *a, int n, int i)
{
	int l = left(i);
	int r = right(i);
	int g;

	g = (l <= n && a[l] > a[i]) ? l : i;
	g = (r <= n && a[r] > a[g]) ? r : g;

	if (g != i) {
		SWAP(a[i], a[g], l);
		max_heapify(a, n, g);
	}
}

int
main(void)
{
	int i, n, p;
	int *a;

	scanf("%d", &n);
	n += 1; /* 1 origin */

	a = (int *)malloc(sizeof(int) * n);
	if (a == NULL)
		return 1;

	for (i = 1; i < n; i++)
		scanf("%d", a + i);

	for (i = n/2; i > 0; i--)
		max_heapify(a, n, i);

	for (i = 1; i < n; i++)
		printf(" %d", a[i]);
	printf("\n");

	return 0;
}

