object Main extends App {
  val n: Int = io.StdIn.readLine().toInt
  val arr: Array[Int] = io.StdIn.readLine().split(" ") map(_.toInt)

  def partition(a: Array[Int], pv: Int, r:Int): Int = {
    var p: Int = pv

    for(i <- p until r) {
      val (ai, ap, ar) = (a(i), a(p), a(r))
      if (i == p) {if (ai <= ar) p += 1}
      else {if (ai <= ar && ai < ap) {a(i) = ap; a(p) = ai; p += 1}}
    }
    val ap = a(p)
    a(p) = a(r); a(r) = ap
    p
  }

  val p: Int = partition(arr, 0, n-1)
  println(arr.slice(0, p).mkString(" ") + " [" + arr(p) + "] " + arr.slice(p+1, n).mkString(" "))
}