#include<stdio.h>
#define MAX 1000000000
int A[MAX],n;


	int parent(int i){
		return i/2;
	}
	int left(int i){
		return 2* i;
	}
	int right(int i){
		return 2*i +1;
	}
	maxHeapfy(int i){
		int largest,tmp;
		int l = left(i);
		int r = right(i);
		if(l <= n &&A[l] > A[i])
		largest = l;
		else largest = i;
		if(r <= n &&A[r] > A[largest])
		largest = r;
		if(largest != i){
			A[i] = A[largest];
			tmp = A[i];
			A[largest] = tmp;
		}
		maxHeapfy(largest);
	}
	buildmaxHeap(){
		int i;
		for(i = n / 2;i >= 1;i--){
			maxHeapfy(i);
		}
	}
	
	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;
}