import scala.io.StdIn.{readInt, readLine}

object Main extends App {
  import Partition.partition

  val n  = readInt()
  val xs = readLine().split(' ').map { _.toInt }

  val q = partition(xs)

  val ys = xs.iterator.zipWithIndex.map {
    case (x, i) => if (i == q) s"[$x]" else x.toString
  }

  println(ys.mkString(" "))
}

object Partition {
  def partition(xs: Array[Int]): Int = {
    def run(xs: Array[Int], p: Int, r: Int): Int = {
      val x = xs(r)
      var i = p - 1
      var j = p
      while (j < r) {
        if (xs(j) <= x) {
          i += 1
          swap(xs, i, j)
        }
        j += 1
      }

      val q = i + 1
      swap(xs, q, r)
      q
    }

    run(xs, 0, xs.length - 1)
  }

  private def swap(xs: Array[Int], a: Int, b: Int): Unit = {
    val temp = xs(a)
    xs(a) = xs(b)
    xs(b) = temp
  }
}

