#include<stdio.h>

int H[500001],n;

void MaxHeapify(int pos){
	int L=pos*2, R=pos*2+1,largest,tmp;

	if(L<=n && H[L]>H[pos])largest=L;
	else largest=pos;

	if(R<=n && H[R]>H[largest])largest=R;

	if(largest!=pos){
		//swap
		tmp=H[pos];
		H[pos]=H[largest];
		H[largest]=tmp;

		MaxHeapify(largest);
	}

	return;
}

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

	for(i=n/2;i>0;i--)MaxHeapify(i);
	for(i=1;i<=n;i++){
		printf(" %d",H[i]);
	}printf("\n");


	return 0;
}
