#include <stdio.h>
#define MAX 2000000000
void maxHeapify(int);
int A[MAX+1];
int H;

int main(void)
{
	int i,j;

	scanf("%d",&H);
	
	for(i=1;i<H+1;i++){
	scanf("%d", &A[i]);
	}
	
	for(i=H/2;i>=1;i--){
	maxHeapify(i);
	//printf("max-heapifing...\n");
	}
	
	for(i=1;i<H+1;i++){
		printf(" %d",A[i]);
	}
	printf("\n");
	
}

void maxHeapify(int i){
	int tmp;
	int max;
	if(i*2<H+1 && A[i] < A[i*2]){
		max = i*2;
		//printf("left is largest\n");
	}
	
	else {
		max = i;
	//	printf("i is largest\n");
	}
	if(i*2+1 < H+1 && A[max] < A[i*2+1]){
		max = i*2+1;
	//	printf("right is largest\n");
	}
	
	if(max != i ){
		tmp  = A[max];
		A[max] = A[i];
		A[i] = tmp; 
		maxHeapify(max);
	}
}

