import heapq



N = int(input())

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

ans = []



a = [(-x, x) for x in H]

heapq.heapify(a)



for i in range(N):

    print(" {}".format(a[i][1]), end = "")

print()

H = int(input())

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



def MaxHeapify(a, i):

    li, ri = 2*i+1, 2*i+2

    if li < H and a[li] > a[i]:

        largest = li

    else:

        largest  = i

        

    if ri < H and a[ri] > a[largest]:

        largest = ri

        

    if a[largest] > a[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(A)



print(' ', end='')

print(*A)

import heapq

N = int(input())
H = list(map(int, input().split()))
ans = []
a = [(-x, x) for x in H]
heapq.heapify(a)
for i in range(N):
    print(" {}".format(a[i][1]), end="")
print()

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


def MaxHeapify(a, i):
    li, ri = 2 * i + 1, 2 * i + 2
    if li < H and a[li] > a[i]:
        largest = li
    else:
        largest = i
    if ri < H and a[ri] > a[largest]:
        largest = ri
    if a[largest] > a[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(A)
print(" ", end="")
print(*A)

-import heapq +H = int(input()) +A = list(map(int, input().split())) -N = int(input()) -H = list(map(int, input().split())) -ans = [] -a = [(-x, x) for x in H] -heapq.heapify(a) -for i in range(N): -    print(" {}".format(a[i][1]), end="") -print() + +def MaxHeapify(a, i): +    li, ri = 2 * i + 1, 2 * i + 2 +    if li < H and a[li] > a[i]: +        largest = li +    else: +        largest = i +    if ri < H and a[ri] > a[largest]: +        largest = ri +    if a[largest] > a[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(A) +print(" ", end="") +print(*A)