#include <stdio.h>
#include <stdlib.h>

int n;

void maxHeapify(long* array, int i) {
	int l, r, largest;
	long work;
	
	l = i * 2;
	r = i * 2 + 1;
	
	if (l <= n && array[l] > array[i]) {
		largest = l;
	} else {
		largest = i;
	}
	
	if (r <= n && array[r] > array[largest]) {
		largest = r;
	}
	
	if (largest != i) {
		work = array[i];
		array[i] = array[largest];
		array[largest] = work;
		maxHeapify(array, largest);
	}
}

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

int main(void) {
	int i;
	long *heap = NULL;
	scanf("%d", &n);
	
	heap = (long*)malloc(sizeof(long) * (n + 1));
	
	for (i = 1; i <= n; i++) {
		scanf("%ld", &heap[i]);
	}
	
	buildMaxHeap(heap);

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

	return 0;
}



/*
1
23
4567
89101112
*/