length = int(eval(input()))

target = [int(n) for n in input().split(" ")]

splitter = target[-1]

border = 0

for i, n in enumerate(target):

    if n <= splitter:

        target[i], target[border] = target[border], target[i]

        border += 1

target[border - 1] = [splitter]

print((*target))
def partition(li, m, length):

    ini_i = 0

    target = li[length - 1]

    for i, n in enumerate(li[:length - 1]):

        if n <= target:

            li[ini_i], li[i] = li[i], li[ini_i]

            ini_i += 1

    li[ini_i], li[-1] = [li[-1]], li[ini_i]

    print((*li))

length = int(eval(input()))

li = [int(n) for n in input().split(" ")]

partition(li, 0, len(li))

length = int(eval(input()))
target = [int(n) for n in input().split(" ")]
splitter = target[-1]
border = 0
for i, n in enumerate(target):
    if n <= splitter:
        target[i], target[border] = target[border], target[i]
        border += 1
target[border - 1] = [splitter]
print((*target))

def partition(li, m, length):
    ini_i = 0
    target = li[length - 1]
    for i, n in enumerate(li[: length - 1]):
        if n <= target:
            li[ini_i], li[i] = li[i], li[ini_i]
            ini_i += 1
    li[ini_i], li[-1] = [li[-1]], li[ini_i]
    print((*li))


length = int(eval(input()))
li = [int(n) for n in input().split(" ")]
partition(li, 0, len(li))

+def partition(li, m, length): +    ini_i = 0 +    target = li[length - 1] +    for i, n in enumerate(li[: length - 1]): +        if n <= target: +            li[ini_i], li[i] = li[i], li[ini_i] +            ini_i += 1 +    li[ini_i], li[-1] = [li[-1]], li[ini_i] +    print((*li)) + + -target = [int(n) for n in input().split(" ")] -splitter = target[-1] -border = 0 -for i, n in enumerate(target): -    if n <= splitter: -        target[i], target[border] = target[border], target[i] -        border += 1 -target[border - 1] = [splitter] -print((*target)) +li = [int(n) for n in input().split(" ")] +partition(li, 0, len(li))