#include<stdio.h>

#define HEAP_SIZE 2000000
long int heap[HEAP_SIZE+1];
int heap_size;

void maxHeapify(long int heap[], int i)
{
	int l = i * 2;
	int r = i * 2 + 1;
	int largest;
	long int w;

	if((l <= heap_size) && (heap[l] > heap[i])){
		largest = l;
	}else{
		largest = i;
	}
	if((r <= heap_size) && (heap[r] > heap[largest])){
		largest = r;
	}
	if(largest != i){
		w = heap[i];
		heap[i] = heap[largest];
		heap[largest] = w;
		maxHeapify(heap, largest);
	}

	return;
}

void buildMaxHeap(long int heap[])
{
	int i;
	for(i = heap_size / 2; 1 <= i; i--){
		maxHeapify(heap, i);
	}
	return;
}

int main()
{
	int i;

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

	buildMaxHeap(heap);

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

	return(0);
}