#include <stdio.h>
#define MAX 2000000
void heap(int i,int A[]);
int H;
int A[MAX+1];
int main(void){
	int i;
	scanf("%d",&H);
	int A[H];
	for(i=1;i<=H;i++){
		scanf("%d",&A[i]);
	}
	
	for(i=H/2;i>=1;i--){
		heap(i,A);
	}
	
	for(i=1;i<=H;i++){
		printf(" ");
		printf("%d",A[i]);
	}
	printf("\n");
	return 0;
}

void heap(int i,int A[]){
	int max;
	int a,b;
	int tmp;
	a=2*i;
	b=2*i+1;
	if(a<=H && A[a]>A[i]){
		max=a;
	}
	else {
		max=i;
	}
	
	if(b<=H && A[b]>A[max]){
		max=b;
	}
	if(max!=i){
		tmp=A[i];
		A[i]=A[max];
		A[max]=tmp;
		heap(max,A);
	}
}
