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

  def partition(arr: Array[Int], i: Int, p: Int, r:Int): (Array[Int], Int) = {
    val a: Array[Int] = arr.clone()
    var p: Int = 0

    for(i <- 0 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
    
    (a,p)
  }

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