
#include <stdio.h>

int parentNode(int i);
int leftChild(int i);
int rightChild(int i);
void maxHeapify(int i);
void buildMaxHeap(void);

int A[500001];
int n;

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;
	
}

int parentNode(int i){
	return (int)i/2;
}

int leftChild(int i){
	return 2*i;
}

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

void maxHeapify(int i){
	int l = leftChild(i);
	int r = rightChild(i);
	int largest, tmp;
	
	if(l <= n && A[l] > A[i]){
		largest = l;	
	}else{
		largest = i;
	}
	if(r <= n && A[r] > A[largest]){
		largest = r;	
	}
	if(largest != i){
		tmp = A[i];
		A[i] = A[largest];
		A[largest] = tmp;
		maxHeapify(largest);
	}
}

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