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


n = gets.chomp.to_i
input_array = gets.chomp.split.map(&:to_i)

q = partition(input_array, 0, n-1)
n.times do |i|
  if i == q
    print "[#{input_array[i]}]"
  else
    print input_array[i]
  end
  if i < (n-1)
    print " "
  end
end
print "\n"