def partition(array, p, r)
  x = array[r]
  i = p - 1
  (p..r-1).each do |index|
    if (array[index] <= x) then
      i += 1
      array[i], array[index] = array[index], array[i]
    end
  end
  array[i+1], array[r] = array[r], array[i+1]
  return i+1
end

n = gets.chomp.to_i
array = gets.chomp.split.map(&:to_i)
pivot = array[n-1]
partition(array, 0, n-1)

found = false
array = array.reverse.map do |item|
  if (!found && item == pivot) then
    found = true
    "[#{item}]"
  else
    item.to_s
  end
end
puts array.reverse.join(' ')