#include <stdio.h>
#include <math.h>
#define MAX 500001

int A[MAX];
int n;

void maxHeapify(int i){

	int temp,largest;
	int l = 2 * i;
	int r = 2 * i + 1;

	//左の子、自分、右の子で値が最大のノードを選ぶ
	if(l <= n && A[l] > A[i]) largest = l;
	else largest = i;

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

	if(largest != i){
		temp = A[i];
		A[i] = A[largest];
		A[largest] = temp;
		maxHeapify(largest);
	}
}

void buildMaxHeap(){
	int j;
	for( j = (int)n/2; j >= 1; j--) maxHeapify(j);
}

int main(){
	
	int i;

	scanf("%d",&n);

	for(i = 1; i <= n; i++) scanf("%d",&A[i]);
	buildMaxHeap();
	for(i = 1; i <= n; i++) printf(" %d",A[i]);
	printf("\n");

	return 0;
}
