#include<stdio.h>

#define int long long

int A[500010];
int n;

void swap(int *a, int *b)
{
	int t = *a;
	*a = *b;
	*b = t;
}

void maxHeap(int p)
{
	int parent, child;
	for (parent = p; parent * 2 <= n; parent = child)
	{
		child = 2 * parent;
		if (child != n && A[child + 1] > A[child])
			child++;
		if (A[parent] > A[child])
			break;
		else
			swap(&A[parent], &A[child]);
	}
}

int main()
{
	scanf("%lld", &n);
	for (int i = 1; i <= n; i++)
		scanf("%lld", &A[i]);
	for (int i = n / 2; i > 0; i--)
		maxHeap(i);
	for (int i = 1; i <= n; i++)
		printf(" %lld", A[i]);
	printf("\n");
}
