def partition(a, p, r)
  x = a[r]
  i = p-1
  (p..r-1).each do |j|
    next unless a[j] <= x
    i = i+1
    tmp = a[i]
    a[i] = a[j]
    a[j] = tmp
  end
  tmp = a[i+1]
  a[i+1] = "[#{a[r]}]"
  a[r] = tmp
  return i+1
end
_, a = readlines.map(&:chomp).map {|l| l.split.map(&:to_i)}
partition(a, 0, a.size - 1)
puts a.join(' ')
