def partition(a)
  x = a.pop
  mid = a.length/2
  left = a[0..mid]
  right = a[mid+1..-1]
  i = 0
  j = mid + 1
  loop do
    next if left[i] < x
    if left.size - 1 < i
      j.upto(right.size-1) do |k|
        if right[k] < x
          left.push(right[k])
          right.delete_at(k)
        end
      end
      break
    else
      j += 1 until right[j] < x || right.size - 1 < j
      unless right.size - 1 < j
        left[i], right[j] = right[j], left[i] 
      else
        left.push(right[i])
        right.delete_at(i)
      end
    end
  end
      
  result = left.join(' ') + " [#{x}] " + right.join(' ')
  result
end

n = gets.to_i
a = gets.chomp.split.map(&:to_i)
puts partition(a)