#include<stdio.h>
#define maxH 500001


void maxHeapify(int A[],int i,int H){
	int l=i*2;
	int r=l+1;
	int largest;
	
	if(l<=H && A[l]>A[i])largest=l;
	else largest=i;
	
	if(r<=H && A[r]>A[largest])largest=r;
	
	if(largest!=i){
		int temp=A[i];
		A[i]=A[largest];
		A[largest]=temp;
		maxHeapify(A,largest,H);
	}
}

void buildMaxHeap(int A[],int H){
	int i;
	for(i=H/2;i>0;i--)maxHeapify(A,i,H);
}

int main(void){
	int A[maxH];
	int i,H;
	scanf("%d",&H);
	for(i=1;i<=H;i++)scanf("%d",&A[i]);
	buildMaxHeap(A,H);
	for(i=1;i<=H;i++)printf(" %d",A[i]);
	printf("\n");
	return 0;
}