def partition(a, p, r)
	x = a[r]
	i = p - 1
	for j in p..r
		if a[j] <= x
			i += 1
			a[j],a[i] = a[i],a[j]
		end
	a[r],a[i+1] = a[i+1],a[r]
	end
	return i + 1
end

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

i = partition(a,0,n-1)

puts a[0,i-1].join(" ") + " [" + a[i].to_s + "] " + a[i+1,n-i-1].join(" ")