fun main(args: Array<String>) {
    readLine()!!.toInt()
    val list = readLine()!!.split(' ').map { it.toInt() }
    val (parted, q) = partition(list)

    println(parted.mapIndexed { i, v -> if (i == q) "[$v]" else "$v" }.joinToString(" "))
}

private fun partition(list: List<Int>): Pair<List<Int>, Int> {
    val p = 0
    val r = list.lastIndex
    val mutableList = list.toMutableList()

    val x = mutableList[r]
    var i = p - 1

    for (j in p until r) {
        if (mutableList[j] <= x) {
            i++
            val t = mutableList[i]
            mutableList[i] = mutableList[j]
            mutableList[j] = t
        }
    }

    val t = mutableList[i + 1]
    mutableList[i + 1] = mutableList[r]
    mutableList[r] = t

    return mutableList to i + 1
}
