import scala.io.StdIn.{readInt, readLine}
object Main {
    def main(args: Array[String]): Unit = {
        val len = readInt
        val arr = readLine.split(" ").map(_.toInt)
        val pivot = partition(arr, len)
        val ret = arr.zipWithIndex.map{case (v, i) =>
            if (i == pivot) "[" + v.toString + "]" else v.toString
        }
        println(ret.mkString(" "))
    }
    def partition(arr: Array[Int], len: Int): Int = {
        val pivot = arr(len - 1)
        def go(sep: Int, seek: Int): Int = {
            if (seek == len - 1) {
                swap(arr, sep + 1, seek)
                sep + 1
            } else {
                if (arr(seek) > pivot) {
                    go(sep, seek + 1)
                } else {
                    swap(arr, sep + 1, seek)
                    go(sep + 1, seek + 1)
                }
            }
        }
        go(-1, 0)
    }
    def swap(arr: Array[Int], a: Int, b: Int): Unit = {
        val tmp = arr(a)
        arr(a) = arr(b)
        arr(b) = tmp
    }
}
