fun main(args: Array<String>) {
    readLine()
    val array = readLine()
        ?.split(" ".toRegex())
        ?.map(Integer::parseInt)
        ?.toIntArray()
        ?: intArrayOf()
    val mid = array.partition(0, array.lastIndex)
    println(array.mapIndexed { index, value ->
        if (index == mid) {
            "[$value]"
        } else {
            value.toString()
        }
    }.joinToString(" "))
}

fun IntArray.partition(p: Int, r: Int): Int {
    val x = this[r]
    var i = p - 1
    for (j in p until r) {
        if (this[j] <= x) {
            i++
            swap(i, j)
        }
    }
    swap(i + 1, r)
    return i + 1
}

fun IntArray.swap(i: Int, j: Int) {
    val tmp = this[i]
    this[i] = this[j]
    this[j] = tmp
}
