def partition(array, p, r)
  x = array[-1]
  i = p - 1
  p.upto(r - 2) do |index|
    if array[index] <= x
      i += 1
      array[i], array[index] = array[index], array[i]
    end
  end
  array[-1], array[i + 1] = array[i + 1], array[-1]
  [array, i + 1]
end

n = gets.to_i
input = gets.split(' ').map(&:to_i)
result = partition input, 0, n
result[0][result[1]] = "[#{result[0][result[1]]}]"
puts result[0].join ' '
