#include<stdio.h>

#define SIZE 500000

int heap[SIZE];

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

void buildMaxHeap(int n){
	int i;
	for( i = SIZE/2; i >= 1; i-- ){
		maxHeapify(i, n);
	}
}

void output(int k, int n){
	printf("node %d: key = %d, ", k, heap[k]);
	if( k/2 > 0 ){ printf("parent key = %d, ", heap[k/2]); }
	if( k*2 <= n ){ printf("left key = %d, ", heap[k*2]); }
	if( k*2 + 1 <= n ){ printf("right key = %d, ", heap[k*2+1]); }
	printf("\n");
	return;
}

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