import sys



def partition(A, p, r):

    x = A[r]

    i = p - 1

    for j in range(p, r):

        if A[j] <= x:

            i += 1

            swap(A, i, j)

    swap(A, i + 1, r)



    return i + 1



def swap(array, i, j):

    tmp = array[i]

    array[i] = array[j]

    array[j] = tmp



if __name__ == "__main__":

    lines = sys.stdin.readlines()

    A = list(map(int, lines[1].split()))

    q = partition(A, 0, len(A) - 1)



    pre = ""

    suf = ""

    for i in range(len(A) - 1):

        if i == q:

            pre = "["

            suf = "]"

        print(pre + str(A[i]) + suf, end=' ')

        pre = ""

        suf = ""

    print(A[i + 1])
import sys



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]



    return i + 1



if __name__ == "__main__":

    lines = sys.stdin.readlines()

    A = list(map(int, lines[1].split()))

    q = partition(A, 0, len(A) - 1)

    A = list(map(str, A))

    A[q] = "[" + A[q] + "]"

    print(" ".join(A))
import sys


def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            swap(A, i, j)
    swap(A, i + 1, r)
    return i + 1


def swap(array, i, j):
    tmp = array[i]
    array[i] = array[j]
    array[j] = tmp


if __name__ == "__main__":
    lines = sys.stdin.readlines()
    A = list(map(int, lines[1].split()))
    q = partition(A, 0, len(A) - 1)
    pre = ""
    suf = ""
    for i in range(len(A) - 1):
        if i == q:
            pre = "["
            suf = "]"
        print(pre + str(A[i]) + suf, end=" ")
        pre = ""
        suf = ""
    print(A[i + 1])

import sys


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]
    return i + 1


if __name__ == "__main__":
    lines = sys.stdin.readlines()
    A = list(map(int, lines[1].split()))
    q = partition(A, 0, len(A) - 1)
    A = list(map(str, A))
    A[q] = "[" + A[q] + "]"
    print(" ".join(A))

-            swap(A, i, j) -    swap(A, i + 1, r) +            A[i], A[j] = A[j], A[i] +    A[i + 1], A[r] = A[r], A[i + 1] - - -def swap(array, i, j): -    tmp = array[i] -    array[i] = array[j] -    array[j] = tmp -    pre = "" -    suf = "" -    for i in range(len(A) - 1): -        if i == q: -            pre = "[" -            suf = "]" -        print(pre + str(A[i]) + suf, end=" ") -        pre = "" -        suf = "" -    print(A[i + 1]) +    A = list(map(str, A)) +    A[q] = "[" + A[q] + "]" +    print(" ".join(A))