import std.stdio;
import std.conv;
import std.algorithm;
import std.range;
import std.string;
size_t partition(T)(T A, size_t p, size_t r)
{
    auto x = A[r];
    auto i = p - 1;
    for (auto j = p; j < r; j++)
    {
        if (A[j] <= x)
        {
            i++;
            A[i].swap(A[j]);
        }
    }
    A[i + 1].swap(A[r]);
    return i + 1;
}
void main()
{
    readln();
    int[] a = readln().strip.split(' ').map!(x => x.to!int).array;
    auto ad = partition(a, 0, a.length - 1);
    foreach (i, j; a)
    {
        if (i)
            write(" ");
        if (i == ad)
            write("[");
        write(j);
        if (i == ad)
            write("]");
    }
}

