#include<stdio.h>
#define MAX 500000
int n;
void maxHeapify(int *A,int i){
	int l,r;
	int largest;
	int change;
	l = i*2;
	r = i*2+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){
		change = A[i];
		A[i] = A[largest];
		A[largest] = change;
		maxHeapify(A,largest);
	}
}

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

int main(){
	int i;
	int H[MAX];
	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;
}
