def partition(A,p,r):

    x = A[r]

    i = p 

    j = p+1

    while i < r:

        if A[i] <= x:

            i += 1

            if i == j:

                j += 1

        else:

            while j <= r:

                if A[j] <= x:

                    if j == r:

                        temp = A[j]

                        A[j] = A[i]

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

                        i += 1

                        i = r

                        break 

                    temp = A[j]

                    A[j] = A[i]

                    A[i] = temp

                    i += 1

                    break

                else:

                    j += 1

    return A

    

    

n = int(eval(input()))

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

print((*partition(A,0,n-1)))
def partition(A,p,r):

    x = A[r]

    i = p

    for j in range(p,r):

        if A[j] <= x:

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

            i += 1

    A[r],A[i] = A[i],A[r]

    return i



n = int(eval(input()))

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

i = partition(A,0,n-1)

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

print((*A))
def partition(A, p, r):
    x = A[r]
    i = p
    j = p + 1
    while i < r:
        if A[i] <= x:
            i += 1
            if i == j:
                j += 1
        else:
            while j <= r:
                if A[j] <= x:
                    if j == r:
                        temp = A[j]
                        A[j] = A[i]
                        A[i] = "[{}]".format(temp)
                        i += 1
                        i = r
                        break
                    temp = A[j]
                    A[j] = A[i]
                    A[i] = temp
                    i += 1
                    break
                else:
                    j += 1
    return A


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

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


n = int(eval(input()))
A = list(map(int, input().split()))
i = partition(A, 0, n - 1)
A[i] = "[{}]".format(A[i])
print((*A))

-    j = p + 1 -    while i < r: -        if A[i] <= x: +    for j in range(p, r): +        if A[j] <= x: +            A[j], A[i] = A[i], A[j] -            if i == j: -                j += 1 -        else: -            while j <= r: -                if A[j] <= x: -                    if j == r: -                        temp = A[j] -                        A[j] = A[i] -                        A[i] = "[{}]".format(temp) -                        i += 1 -                        i = r -                        break -                    temp = A[j] -                    A[j] = A[i] -                    A[i] = temp -                    i += 1 -                    break -                else: -                    j += 1 -    return A +    A[r], A[i] = A[i], A[r] +    return i -print((*partition(A, 0, n - 1))) +i = partition(A, 0, n - 1) +A[i] = "[{}]".format(A[i]) +print((*A))