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

  def partition(a: Vector[Int], i: Int, p: Int, r:Int): (Vector[Int], Int) = {
    if (i >= r) {val (ap,ar) = (a(p),a(r)); (a.updated(p,ar).updated(r,ap), p)}
    else if (i == p) partition(a, i+1, if (arr(i) <= arr(r)) p+1 else p, r)
    else {
      val ai = a(i)
      val ap = a(p)
      if (ai <= a(r) && ai < ap) partition(a.updated(i,ap).updated(p,ai), i+1, p+1, r)
      else partition(a, i+1, p, r)
    }
  }

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