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

class Array
  def partition
    x = self[-1]
    i = -1
    (size - 1).times do |j|
      if self[j] <= x
        i += 1
        self[i], self[j] = self[j], self[i]
      end
    end
    self[i + 1], self[-1] = self[-1], self[i + 1]
    i + 1
  end
end

r = a.partition
puts a[0, r].join(" ") + " [#{a[r]}] " + a[r + 1..-1].join(" ")
