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

n = gets.chomp.to_i
a = gets.chomp.split(" ").map(&:to_i)
q = partition(a, 0, n - 1)
a[q] = "[#{a[q]}]"
puts a.join(" ")