H = int(eval(input()))

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

def maxHeapify(A, i):

    l = i * 2 + 1

    r = i * 2 + 2

    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, -1, -1):

        maxHeapify(A, i)

buildMaxHeap(L)

print(("", *L))



H = int(eval(input()))

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

def maxHeapify(A, i):

    l = i * 2 + 1

    r = i * 2 + 2

    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, -1, -1):

        maxHeapify(A, i)

buildMaxHeap(L)

print(("", " ".join(map(str, L))))



H = int(eval(input()))
L = list(map(int, input().split()))


def maxHeapify(A, i):
    l = i * 2 + 1
    r = i * 2 + 2
    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, -1, -1):
        maxHeapify(A, i)


buildMaxHeap(L)
print(("", *L))

H = int(eval(input()))
L = list(map(int, input().split()))


def maxHeapify(A, i):
    l = i * 2 + 1
    r = i * 2 + 2
    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, -1, -1):
        maxHeapify(A, i)


buildMaxHeap(L)
print(("", " ".join(map(str, L))))

-print(("", *L)) +print(("", " ".join(map(str, L))))