#include <stdio.h>
#define MAX 2000000
void maxHeapify(int);
int left(int);
int right(int);
void swap(int,int);

int n,heap[MAX];

int main(){	
	int i;

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

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


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

	return 0;
}

void maxHeapify(int i){
	int l,r,largest;

	l = left(i);
	r = right(i);

	if(l <= n && heap[l] > heap[i]) largest = l;
	else largest = i;

	if(r <= n && heap[r] > heap[largest]) largest = r;

	if(largest != i){
		swap(i,largest);
		maxHeapify(largest);
	}
}

int left(int i){
	return 2 * i;
}

int right(int i){
	return 2 * i + 1;
}

void swap(int a,int b){
int temp;

temp = heap[a];
heap[a] = heap[b];
heap[b] = temp;

}