#coding:utf-8

H = int(eval(input()))

A = list(map(int,input().split()))



def maxHeapify(A,i):

    l = 2 * i

    r = 2 * i + 1

    if l <= H and A[l-1] > A[i-1]:

        largest = l

    else:

        largest = i

    if r <= H and A[r-1] > A[largest-1]:

        largest = r

    if largest != i:

        A[i-1], A[largest-1] = A[largest-1], A[i-1]

        maxHeapify(A,largest)



def buildMaxHeap(A):

    for i in range(H // 2, 0, -1):

        maxHeapify(A,i)



buildMaxHeap(A)

A = " " + " ".join([str(num) for num in A])

print(A)



#coding: utf-8



def maxHeapify(A, i):

    l = 2 * i

    r = 2 * i  + 1



    if l <= H and A[l] > A[i]:

        largest = l

    else:

        largest = i



    if r <= H and A[r] > A[largest]:

        largest = r



    if largest != i:

        A[i], A[largest] = A[largest], A[i]

        maxHeapify(A, largest)



def buildMaxHeap(A):

    for i in range(H//2, 0, -1):

        maxHeapify(A, i)



H = int(eval(input()))

A = [0] + list(map(int,input().split()))

buildMaxHeap(A)

A.pop(0)

print((" " + " ".join([str(num) for num in A])))



# coding:utf-8
H = int(eval(input()))
A = list(map(int, input().split()))


def maxHeapify(A, i):
    l = 2 * i
    r = 2 * i + 1
    if l <= H and A[l - 1] > A[i - 1]:
        largest = l
    else:
        largest = i
    if r <= H and A[r - 1] > A[largest - 1]:
        largest = r
    if largest != i:
        A[i - 1], A[largest - 1] = A[largest - 1], A[i - 1]
        maxHeapify(A, largest)


def buildMaxHeap(A):
    for i in range(H // 2, 0, -1):
        maxHeapify(A, i)


buildMaxHeap(A)
A = " " + " ".join([str(num) for num in A])
print(A)

# coding: utf-8
def maxHeapify(A, i):
    l = 2 * i
    r = 2 * i + 1
    if l <= H and A[l] > A[i]:
        largest = l
    else:
        largest = i
    if r <= H and A[r] > A[largest]:
        largest = r
    if largest != i:
        A[i], A[largest] = A[largest], A[i]
        maxHeapify(A, largest)


def buildMaxHeap(A):
    for i in range(H // 2, 0, -1):
        maxHeapify(A, i)


H = int(eval(input()))
A = [0] + list(map(int, input().split()))
buildMaxHeap(A)
A.pop(0)
print((" " + " ".join([str(num) for num in A])))

-# coding:utf-8 -H = int(eval(input())) -A = list(map(int, input().split())) - - +# coding: utf-8 -    if l <= H and A[l - 1] > A[i - 1]: +    if l <= H and A[l] > A[i]: -    if r <= H and A[r - 1] > A[largest - 1]: +    if r <= H and A[r] > A[largest]: -        A[i - 1], A[largest - 1] = A[largest - 1], A[i - 1] +        A[i], A[largest] = A[largest], A[i] +H = int(eval(input())) +A = [0] + list(map(int, input().split())) -A = " " + " ".join([str(num) for num in A]) -print(A) +A.pop(0) +print((" " + " ".join([str(num) for num in A])))