def partition(arr, p, r)
  x = arr[r]
  i = p - 1
  p.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]
  return i + 1
end

if $PROGRAM_NAME == __FILE__
  n = gets.to_i
  arr = gets.split.map(&:to_i)
  q = partition(arr, 0, n - 1)
  n.times do |i|
    if i == q
      print "[#{arr[i]}]"
    else
      print arr[i]
    end
    print ' ' if i < n - 1
  end
  puts
end