#include<iostream>
using namespace std;
#define MAX 500005

int H, A[MAX + 1];

void maxHeap(int i) {
	int l, r, large;
	l = 2 * i;
	r = 2 * i + 1;

	if (l <= H&&A[l] > A[i]) {
		large = l;
	}
	else {
		large = i;
	}
	if (r <= H&&A[r] > A[large]) {
		large = r;
	}
	if (large!=i) {
		swap(A[large], A[i]);
		maxHeap(large);
	}
}

int main()
{
	cin >> H;
	for (int i = 1; i <= H; i++) cin >> A[i];
	for (int i = H / 2; i >= 1; i--) maxHeap(i);
	for (int i = 1; i <= H; i++) {
		cout << " " << A[i];
	}
	cout << endl;
    return 0;
}