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

int size, list[500001];

void swap(int* a, int* b) {
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

void maxheapify(int i) {
	int l = i * 2;
	int r = i * 2 + 1;

	int largest_value;
	if (l <= size && list[l] > list[i]) largest_value = l;
	else largest_value = i;
	if (r <= size && list[r] > list[largest_value]) largest_value = r;
	if (largest_value != i) {
		swap(&list[i], &list[largest_value]);
		maxheapify(largest_value);
	}


	/*
	1  maxHeapify(A, i)
2      l = left(i)
3      r = right(i)
4      // select the node which has the maximum value
5      if l ≤ H and A[l] > A[i]
6          largest = l
7      else
8          largest = i
9      if r ≤ H and A[r] > A[largest]
10         largest = r
11
12     if largest ≠ i　// value of children is larger than that of i
13         swap A[i] and A[largest]
14         maxHeapify(A, largest) // call recursively
	*/

}

void buildmaxheap() {
	/*
	1 buildMaxHeap(A)
2    for i = H/2 downto 1
3        maxHeapify(A, i)

	*/
	for (int i = size / 2; i > 0; i--) {
		maxheapify(i);
	}

}

int main() {
	int i, j;
	scanf("%d", &size);

	//scanning
	for (i = 1; i <= size; i++) {
		scanf("%d", &list[i]);
	}

	buildmaxheap(size);


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

	return 0;
}
