#include<stdio.h>
#define MAX 5000
int H[MAX];
maxHeapify(int *, int);
buildMaxHeap(int *,int);
int main() {
	int h,i;

	scanf("%d", &h);
	for (i = 0; i < h; i++) {
		scanf("%d", &H[i]);
	}

	buildMaxHeap(H, h);

	for (i = 0; i < h; i++) {
		printf(" %d", H[i]);
	}
	return 0;
}

maxHeapify(int* A, int i,int H) {
	int l;
	int r;
	int largest;
	int dummy;
	l = A[2 * i];
	r = A[2 * i + 1];
	if (l <= H && A[l] > A[i]) {
		largest = l;
	}
	else largest = i;
	if (r <= H && A[r] > A[largest]) {
		largest = r;
	}
	if (largest != i) {
		dummy = A[i];
		A[i] = A[largest];
		A[largest] = dummy;
		maxHeapify(A, largest, H);
	}
}

buildMaxHeap(int* A,int H){
	int i;
	for (i = 0; i = H / 2; i--) {
		maxHeapify(A, i,H);
	}
}
