#include <stdio.h>
#define MAX 2000000

int id[MAX];

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

void maxHeapify(int n,int i){
	int l,r,bigger,temp;
	l = left(i);
	r = right(i);

	if(l <= n && id[l] > id[i]){
		bigger = l;
	}else bigger = i;

	if(r <= n && id[r] > id[bigger]){
		bigger = r;
	}

	if(bigger != i){
		temp = id[i];
		id[i] = id[bigger];
		id[bigger] = temp;
		maxHeapify(n,bigger);
	}
}

int main(){
	int i,n;

	scanf("%d",&n); /*回数*/

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

	for(i=n/2;i>=1;i--){
		maxHeapify(n,i);
	}

	for(i=1;i<=n;i++){
		printf(" %d",id[i]);
	}

	printf("\n");

	return 0;
}
