n = STDIN.gets.to_i
A = STDIN.gets.split(" ").map(&:to_i)
def pertition(a,p,r)
  i=p-1
  x=a[r]
  for j in p..r
    if a[j] <= x
      i+=1
      tmp=a[i]
      a[i]=a[j]
      a[j]=tmp
    end
  end
  #tmp=a[r]
  #a[r]=a[i+1]
  #a[i+1]=tmp

  a[i]='['+a[i].to_s+']'
  puts a.join(" ")
end
pertition(A,0,A.length-1)