def part(a, q, r)
  x = a[r]
  i = q - 1
  for j in 0...r - q
    if a[q + j] <= x
      i += 1
      a[i], a[q + j] = a[q + j], a[i]
    end
  end
  a[i + 1], a[r] = "[" + a[r].to_s + "]", a[i + 1]
  return i + 1
end
gets
a = gets.split(" ").map(&:to_i)
part(a, 0, a.length - 1)
puts a.join(" ")