n = gets(chomp:true).to_i
a = gets.split.map(&:to_i)

def partition(a, p, r)
  x = a[r]
  i = p - 1
  (p...r).each do |j|
    if a[j] <= x
      i += 1
      a[i], a[j] = a[j], a[i]
    end
  end
  a[i + 1], a[r] = "[#{a[r].to_s}]", a[i + 1]
  puts a.join(" ")
end

partition(a, 0, n - 1)
