from collections import deque



class PriorityQueue:

    def __init__(self):

        self.nodes = []



    def max_heapify(self, i):

        if i >= len(self.nodes): return

        left, right = (i+1)*2-1, (i+1)*2



        largest = i

        if left < len(self.nodes) and self.nodes[i] < self.nodes[left]: largest = left

        if right < len(self.nodes) and self.nodes[largest] < self.nodes[right]: largest = right



        if largest != i:

            self.nodes[i], self.nodes[largest] = self.nodes[largest], self.nodes[i]

            self.max_heapify(largest)



    def print_element(self):

        for node in self.nodes:

            print('', node, end='')

        print('')



n = int(input())

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



pq = PriorityQueue()

pq.nodes = A



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

    pq.max_heapify(i)



pq.print_element()

n = int(input())

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



def max_heapify(i):

    l = (i+1)*2-1

    r = (i+1)*2

    largest = i

    if l < n and H[l] > H[i]:

        largest = l

    else:

        largest = i

    if r < n and H[r] > H[largest]:

        largest = r



    if largest != i:

        H[i], H[largest] = H[largest], H[i]

        max_heapify(largest)



def print_elements(lst):

    for ele in lst:

        print(' {}'.format(ele), end='')

    print('')



for i in range(int(n/2), -1, -1):

    max_heapify(i)



print_elements(H)

from collections import deque


class PriorityQueue:
    def __init__(self):
        self.nodes = []

    def max_heapify(self, i):
        if i >= len(self.nodes):
            return
        left, right = (i + 1) * 2 - 1, (i + 1) * 2
        largest = i
        if left < len(self.nodes) and self.nodes[i] < self.nodes[left]:
            largest = left
        if right < len(self.nodes) and self.nodes[largest] < self.nodes[right]:
            largest = right
        if largest != i:
            self.nodes[i], self.nodes[largest] = self.nodes[largest], self.nodes[i]
            self.max_heapify(largest)

    def print_element(self):
        for node in self.nodes:
            print("", node, end="")
        print("")


n = int(input())
A = list(map(int, input().split(" ")))
pq = PriorityQueue()
pq.nodes = A
for i in range((n - 1) // 2, -1, -1):
    pq.max_heapify(i)
pq.print_element()

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


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


def print_elements(lst):
    for ele in lst:
        print(" {}".format(ele), end="")
    print("")


for i in range(int(n / 2), -1, -1):
    max_heapify(i)
print_elements(H)

-from collections import deque +n = int(input()) +H = list(map(int, input().split(" "))) -class PriorityQueue: -    def __init__(self): -        self.nodes = [] - -    def max_heapify(self, i): -        if i >= len(self.nodes): -            return -        left, right = (i + 1) * 2 - 1, (i + 1) * 2 +def max_heapify(i): +    l = (i + 1) * 2 - 1 +    r = (i + 1) * 2 +    largest = i +    if l < n and H[l] > H[i]: +        largest = l +    else: -        if left < len(self.nodes) and self.nodes[i] < self.nodes[left]: -            largest = left -        if right < len(self.nodes) and self.nodes[largest] < self.nodes[right]: -            largest = right -        if largest != i: -            self.nodes[i], self.nodes[largest] = self.nodes[largest], self.nodes[i] -            self.max_heapify(largest) - -    def print_element(self): -        for node in self.nodes: -            print("", node, end="") -        print("") +    if r < n and H[r] > H[largest]: +        largest = r +    if largest != i: +        H[i], H[largest] = H[largest], H[i] +        max_heapify(largest) -n = int(input()) -A = list(map(int, input().split(" "))) -pq = PriorityQueue() -pq.nodes = A -for i in range((n - 1) // 2, -1, -1): -    pq.max_heapify(i) -pq.print_element() +def print_elements(lst): +    for ele in lst: +        print(" {}".format(ele), end="") +    print("") + + +for i in range(int(n / 2), -1, -1): +    max_heapify(i) +print_elements(H)