def partition(a, p, r)
  x = a[r]
  i = p - 1
  p.upto(r-1).each {|j|
    if a[j] <= x
      i += 1
      a[i], a[j] = a[j], a[i]
    end
  }
  a[i+1], a[r] = a[r], a[i+1]
  i+1
end

n = gets.to_i
a = gets.split.map(&:to_i)
q = partition(a, 0, n - 1)
puts a.map.with_index{|x,i| i == q ? "[#{x}]" : x.to_s}.join(" ")