#include<stdio.h>

int heap[500001];
int have;

int left(int a)
{
	return 2*a;
}
int right(int a)
{
	return 2*a+1;
}

void maxheapify(int i)
{
	int l,r,save,max;
	l=left(i);
	r=right(i);
	max=i;
	if(l<=have&&heap[l]>heap[max])
	{
		max=l;
	}
	if(r<=have&&heap[r]>heap[max])
	{
		max=r;
	}
	if(max!=i)
	{
		save=heap[i];
		heap[i]=heap[max];
		heap[max]=save;
		maxheapify(max);
	}
}

int main(void)
{
	int i,N;
	scanf("%d",&N);
	have=N;

	for(i=1;i<=N;i++)
	{
		scanf("%d",&heap[i]);
	}
	for(i=N/2;i>=1;i--)
	{
		maxheapify(i);
	}
	for(i=1;i<=N;i++)
	{
		printf(" %d",heap[i]);
	}
	printf("\n");
	return 0;
}

