class Array
  def swap!(a, b)
    raise ArgumentError unless a.between?(0, self.count-1) && b.between?(0, self.count-1)

    self[a], self[b] = self[b], self[a]

    self
  end

  def swap(a, b)
    self.dup.swap!(a, b)
  end
end

n = gets.to_i
@arr = gets.split.map(&:to_i)

def partition(p, r)
  x = @arr[r]
  i = p - 1
  (p...r).each do |j|
    if @arr[j] <= x
      i += 1
      @arr.swap!(i, j)
    end
  end
  @arr.swap!(i+1, r)
  
  i+1
end

q = partition(0, n - 1)

n.times do |i|
  print " " unless i == 0
  print "[" if i == q
  print @arr[i]
  print "]" if i == q
end 

print "\n"
