/* ALDS1_9_A: CompleteBinaryTree */
/* 20181129 AOJ */

#define _CRT_SECURE_NO_WARNINGS
#define H 500000
#include <stdio.h>

void maxHeapify(int *heap, int i);
void buildMaxHeap(int *heap);
void swap(int *x, int * y);

int n;
int main(void) {
	int i;
	int heap[H] = {0};

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

	buildMaxHeap(heap);

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

	printf("\n");

	return 0;
}

void maxHeapify(int *heap, int i) {
	int left, right, maxId;
	
	left = 2 * i;
	right = left + 1;
	if(left <= n && heap[left] > heap[i]) maxId = left;
	else maxId = i;

	if(right <= n && heap[right] > heap[maxId]) maxId = right;

	if(maxId != i) {
		swap(heap + i, heap + maxId);
		maxHeapify(heap, maxId);
	}
}

void buildMaxHeap(int *heap) {
	int id;
	for(id = n / 2; id > 0; id--) {
		maxHeapify(heap, id);
	}
}

void swap(int *x, int * y) {
	int temp = *x;
	*x = *y;
	*y = temp;
}
