//Maximum Heap
#include<stdio.h>
#include<limits.h>

#define MAX 2000000

void maxHeapify(int);

int h,H[MAX+1];

int main(){
	int i;

	//input
	scanf("%d",&h);

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


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


	//output
	for(i=1;i<=h;i++){
		printf(" %d",H[i]);
	}
	printf("\n");

	return 0;
}


void maxHeapify(int i){
	int left,right,largest,Ponta;

	left=2*i;
	right=2*i+1;

	if(left<= h && H[left] >H[i]){
		largest=left;
	}
	else{
		largest=i;
	}
	if(right<= h && H[right] >H[largest]){
		largest=right;
	}
	if(largest!=i){
		Ponta=H[i];
		H[i]=H[largest];
		H[largest]=Ponta;
		maxHeapify(largest);
	}
}

