#include <stdio.h>          // printf(), getc()
#include <ctype.h>          // isdigit()

#define MAX_N 500000
#define swap(a, b) { int t; t = a; a = b; b = t; }

void
max_heapify(int a[], int h, int i)
{
	int largest;

	int l = 2 * i;
	int r = 2 * i + 1;

	/* ????????????????????????????????§???????????§???node????????¶ */
	if (l <= h && a[l] > a[i])
		largest = l;
	else
		largest = i;

	if (r <= h && a[r] > a[largest])
		largest = r;

	if (largest != i) /* ????????????????????§????????´?????? */
	{
		swap(a[i], a[largest]);
		max_heapify(a, h, largest);
	}
}

int
main(int argc, char** argv)
{
	int a[MAX_N + 1];
	int h;
	int n, s;
	int i;
	int c;

	h = 0;
	c = getc(stdin);
	while (isdigit(c))
	{
		h = 10 * h + c - '0';
		c = getc(stdin);
	}

	if (c == '\n')
		c = getc(stdin);

	for (i = 1; i <= h; ++i)
	{
		while (c == ' ')
			c = getc(stdin);

		if (c == '-')
		{
			s = -1;
			c = getc(stdin);
		}
		else
			s = 1;

		n = 0;
		while (isdigit(c))
		{
			n = 10 * n + c - '0';
			c = getc(stdin);
		}

		a[i] = s * n;
	}

	for (i = h / 2; i >= 1; --i)
		max_heapify(a, h, i);

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

	printf("\n");
	return 0;
}