def partition(arr, p, r)
  x = arr[r]
  i = p-1
  p.upto(r-1) { |j|
    if arr[j] <= x
      i += 1
      exchange(arr, i, j)
    end
  }
  exchange(arr, i+1, r)
  return i+1
end

def exchange(arr, i, j)
  buf = arr[i]
  arr[i] = arr[j]
  arr[j] = buf
end

n = gets.to_i
arr = gets.chomp.split.map(&:to_i)
idx = partition(arr, 0, arr.size-1)

puts arr.map.with_index { |x, i| (i == idx) ? "[#{x}]" : x }.join(' ')