import sys





def partition(A, p, r):

    # 要素の最後をxに代入

    x = A[r]

    i = p-1

    ans = ""



    for j in range(r):

        if A[j] <= x:

            i += 1

            tmpA = A[i]

            A[i] = A[j]

            A[j] = tmpA



    tmpB = A[i+1]

    A[i+1] = A[r]

    A[r] = tmpB

    for j in range(n):

        if j > 0:

            ans += " "

        if j == i+1:

            ans += "["+str(A[j])+"]"

        else:

            ans += str(A[j])

    print(ans)





n = int(eval(input()))

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

r = len(A)-1

p = 0

partition(A, p, r)

# for i in range(n):

#     if A[i] == A[r]:

#         sys.stdout.write('[' + str(A[i])+']' + ' ')

#     else:

#         sys.stdout.write(str(A[i]) + ' ')

# print()



def partition(A, p, r):

    x = A[r]

    i = p-1

    for j in range(p, r):

        if A[j] <= x:

            i += 1

            A[i], A[j] = A[j], A[i]

    A[i+1], A[r] = A[r], A[i+1]

    A[i+1] = "[{}]".format(A[i+1])





n = int(eval(input()))

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

partition(A, 0, n-1)

print((*A))



import sys


def partition(A, p, r):
    # 要素の最後をxに代入
    x = A[r]
    i = p - 1
    ans = ""
    for j in range(r):
        if A[j] <= x:
            i += 1
            tmpA = A[i]
            A[i] = A[j]
            A[j] = tmpA
    tmpB = A[i + 1]
    A[i + 1] = A[r]
    A[r] = tmpB
    for j in range(n):
        if j > 0:
            ans += " "
        if j == i + 1:
            ans += "[" + str(A[j]) + "]"
        else:
            ans += str(A[j])
    print(ans)


n = int(eval(input()))
A = list(map(int, input().split()))
r = len(A) - 1
p = 0
partition(A, p, r)
# for i in range(n):
#     if A[i] == A[r]:
#         sys.stdout.write('[' + str(A[i])+']' + ' ')
#     else:
#         sys.stdout.write(str(A[i]) + ' ')
# print()

def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i + 1]
    A[i + 1] = "[{}]".format(A[i + 1])


n = int(eval(input()))
A = list(map(int, input().split()))
partition(A, 0, n - 1)
print((*A))

-import sys - - -    # 要素の最後をxに代入 -    ans = "" -    for j in range(r): +    for j in range(p, r): -            tmpA = A[i] -            A[i] = A[j] -            A[j] = tmpA -    tmpB = A[i + 1] -    A[i + 1] = A[r] -    A[r] = tmpB -    for j in range(n): -        if j > 0: -            ans += " " -        if j == i + 1: -            ans += "[" + str(A[j]) + "]" -        else: -            ans += str(A[j]) -    print(ans) +            A[i], A[j] = A[j], A[i] +    A[i + 1], A[r] = A[r], A[i + 1] +    A[i + 1] = "[{}]".format(A[i + 1]) -r = len(A) - 1 -p = 0 -partition(A, p, r) -# for i in range(n): -#     if A[i] == A[r]: -#         sys.stdout.write('[' + str(A[i])+']' + ' ') -#     else: -#         sys.stdout.write(str(A[i]) + ' ') -# print() +partition(A, 0, n - 1) +print((*A))