class A def initialize n, k = gets.chomp.split(' ').map(&:to_i) s = gets.chomp.chars answer = [] stack = [] s.each.with_index(1) do |ch, index| if ch == '(' stack << index else i = stack.pop answer[i] = index answer[index] = i end end puts answer[k] end end A.new