#include<iostream>
using namespace std;

int n, T[500001];

void maxheapify(int i)
{
	int l = 2 * i;
	int r = 2 * i + 1;
	int largest;

	if (l <= n && T[l] > T[i])
		largest = l;
	else
		largest = i;

	if (r <= n && T[r] > T[largest])
		largest = r;

	if (largest != i)
	{
		int t = T[largest];
		T[largest] = T[i];
		T[i] = t;
		maxheapify(largest);
	}	
}

int main()
{
	cin >> n;
	
	for (int i = 1; i <= n; i++)  cin >> T[i];

	for (int i = n / 2; i >= 1; i--)  maxheapify(i);
	
	for (int i = 1; i <= n; i++)  cout <<" "<< T[i];
	cout<<endl;
	
	return 0;
}
