object Main extends App {

  def swap(a: Array[Int], i: Int, j: Int) = {
    val tmp = a(i)
    a(i) = a(j)
    a(j) = tmp
  }

  def partition(a: Array[Int]): Int = {
    val r = a.size - 1
    val p = 0
    val x = a(r)

    var i = p - 1

    for (j <- p until r) {
      if (a(j) <= x) {
        i = i + 1
        swap(a, i, j)
      }
    }
    swap(a, i + 1, r)
    i + 1
  }

  val sc = new java.util.Scanner(Console.in)

  def readInt() = sc.next().toInt

  val a = Array.fill(readInt())(readInt())
  val i = partition(a)
  for (j <- 0 until a.size) {
    if (j > 0) print(' ')
    print(if (j == i) s"[${a(j)}]" else a(j))
  }
  println()
}

