class Partition:

    def partion(self, a, p, r): 

        x = a[-1]

        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[-1] = a[-1], a[i + 1]

        return((a, i))



if __name__ == '__main__':

    n = int(input().rstrip())

    a = [int(x) for x in input().rstrip().split(" ")]

    x = Partition()

    a, i = x.partion(a, 0, n)

    res = []

    for j in range(len(a)):

        if j == i:

            res.append("[{}]".format(a[j]))

        else:

            res.append(str(a[j]))

    print((" ".join(res)))
class Partition:

    def partion(self, 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__':

    n = int(input().rstrip())

    a = [int(x) for x in input().rstrip().split(" ")]

    x = Partition()

    i = x.partion(a, 0, n-1)

    a = [str(i) for i in a]

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

    print((" ".join(a)))
class Partition:
    def partion(self, a, p, r):
        x = a[-1]
        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[-1] = a[-1], a[i + 1]
        return (a, i)


if __name__ == "__main__":
    n = int(input().rstrip())
    a = [int(x) for x in input().rstrip().split(" ")]
    x = Partition()
    a, i = x.partion(a, 0, n)
    res = []
    for j in range(len(a)):
        if j == i:
            res.append("[{}]".format(a[j]))
        else:
            res.append(str(a[j]))
    print((" ".join(res)))

class Partition:
    def partion(self, 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__":
    n = int(input().rstrip())
    a = [int(x) for x in input().rstrip().split(" ")]
    x = Partition()
    i = x.partion(a, 0, n - 1)
    a = [str(i) for i in a]
    a[i] = "[{}]".format(a[i])
    print((" ".join(a)))

-        x = a[-1] +        x = a[r] -        # a[i + 1], a[-1] = a[-1], a[i + 1] -        return (a, i) +        a[i + 1], a[r] = a[r], a[i + 1] +        return i + 1 -    a, i = x.partion(a, 0, n) -    res = [] -    for j in range(len(a)): -        if j == i: -            res.append("[{}]".format(a[j])) -        else: -            res.append(str(a[j])) -    print((" ".join(res))) +    i = x.partion(a, 0, n - 1) +    a = [str(i) for i in a] +    a[i] = "[{}]".format(a[i]) +    print((" ".join(a)))