
import scala.io.StdIn

object Main extends App {
  val n = StdIn.readLine().trim.toInt
  val a = StdIn.readLine().trim.split(' ').map(_.toInt)

  val last = a(n - 1)

  @inline private def swap(from: Int, to: Int): Unit = {
    //println(s"from: $from  to: $to")
    val temp = a(from)
    a(from) = a(to)
    a(to)   = temp
  }
  
  def partition(arr: Array[Int], p: Int, r: Int): Int = {
    var i = p - 1
    (p until r).foreach{ j =>
      while(arr(j) <= last){
        i += 1
        swap(i, j)
        //println(a.toList.mkString(" "))
      }
    }
    swap(i+1, r)
    //println(a.toList.mkString(" "))
    i + 1
  }
  
  val partitionIndex = partition(a, 0, n-1)
  //println(s"num: $partitionIndex")
  val a1 = new Array[Int](partitionIndex)
  Array.copy(a, 0, a1, 0, partitionIndex)
  print(a1.mkString(" ") + (if(a1.nonEmpty) " " else "" ))
  print(s"[$last]")
  val overIndex = partitionIndex + 1
  val a2 = new Array[Int](n - (overIndex))
  Array.copy(a, overIndex, a2, 0, n - (overIndex))
  println( (if(a2.nonEmpty) " " else "" ) + a2.mkString(" "))
  
  
}


