def partition(arr)
  r = arr.size - 1
  x = arr.last
  i = -1
  0.upto(r - 1) do |j|
    if arr[j] <= x
      i += 1
      arr[i], arr[j] = arr[j], arr[i]
    end
  end

  arr[i+1], arr[r] = arr[r], arr[i+1]

  i + 1
end

n = gets.to_s
a = gets.split.map(&:to_i)
pos =  partition(a)
puts a[0...pos].join("\s") + "\s[#{a[pos]}]\s" + a[(pos+1)...a.size].join("\s")