@arr = []
def partition(p, r)
  q = @arr[r]
  i = p - 1
  (p..r-1).each do |j|
    if @arr[j] <= q
      i += 1
      @arr[i], @arr[j] = @arr[j], @arr[i]
    end
  end
  @arr[i+1], @arr[r] = @arr[r], @arr[i+1]
  i+1
end
n = gets.to_i
@arr = gets.split.map(&:to_i)
q = partition(0, n-1)
result = @arr.map(&:to_s)
result[q] = "[#{result[q]}]"
puts result.join(' ')
