#include<stdio.h>
#define MAX 2000000
#define SWAP(a,b) (a += b,b = a - b,a -= b)
int h,A[MAX + 1];

void maxHeapify(int i){
	int left,right,largest;
	left = 2*i;
	right = 2*i+1;
	
	if(left <= h && A[left] > A[i]){
		largest = left;
	}else{
		largest = i;
	}

	if(right <= h && A[right] > A[largest]){
	largest = right;
	}
	
	if(largest != i){
	SWAP(A[i],A[largest]);
	maxHeapify(largest);
	
	}
	
	
}


int main(){
	scanf("%d",&h);
	
	for(int i = 1;i <= h; i++){
	scanf("%d",&A[i]);
	}
	
	for(int i = h/2;i >= 1;i--){
	maxHeapify(i);
	}
	for(int i = 1;i <= h; i++){
	printf(" %d",A[i]);
	}
	printf("\n");
	
	return 0;

}
