#include <stdio.h>

#define N 500001

int n;

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

void maxHeapify(int *H, int i) {
	int l = left(i);
	int r = right(i);
	int largest;
	int tmp;

	if (i <= n/2 && H[l] > H[i]) largest = l;
	else largest = i;
	if (i <= n/2 && H[r] > H[largest]) largest = r;

	if (largest != i) {
		tmp = H[i];
		H[i] = H[largest];
		H[largest] = tmp;
		maxHeapify(H, largest);
	}
}

void buildMaxHeap(int *H) {
	int i;
	for (i = n/2; i >= 1; i--) {
		maxHeapify(H, i);
	}
}

int main() {
	int i;
	int H[N];

	scanf("%d",&n);
	for (i = 1; i <= n; i++) scanf("%d", &H[i]);

	buildMaxHeap(H);
	for (i = 1; i<= n; i++) printf(" %d", H[i]);
	printf("\n");
	return 0;
}