def partition(array, head, tail)
	x = array[tail]
	i = head - 1
	(head...tail).each do |j|
		if array[j] <= x
			i = i + 1
			array[i], array[j] = array[j], array[i]
		end
	end
	array[i + 1], array[tail] = array[tail], array[i + 1]
	i + 1
end

array_size = STDIN.gets.to_i
array = STDIN.gets.split[0...array_size].map(&:to_i)
index = partition(array, 0, array_size - 1)
array[index] = sprintf("[%d]", array[index])
puts array.join(" ")