def partition(a,p,r)
  x = a[r]
  i = p-1
  (p..r-1).each do |j|
    if a[j] <= x
      i = i+1
      a[i],a[j] = a[j],a[i]
    end
  end 
  a[i+1],a[r] = a[r],a[i+1]
  return i+1
end
ab = gets.chomp.to_i
a = gets.chomp.split(" ").map(&:to_i)
ind =  partition(a,0,ab-1)
a.size.times do |d|
  if d != ind
    print("#{a[d]} ")
  elsif(d == ab-1)
    break
  else
    print("[#{a[d]}] ")
  end
end
if ab != ind
  puts("#{a[ab]}")
else
  puts("[#{a[ab]}]")
end