///////////////////////////// No.22 括弧の対応 //入力文字読み取り var inputall = require('fs').readFileSync('/dev/stdin', 'utf8'); //すべて受け取り改行で区切って格納 var input = inputall.split("\n"); //カッコの総数と求めるカッコの場所 var brackets_number = input[0].split(" ").map(Number); //カッコ var brackets = input[1].split(""); // 2次元配列を作成する var brackets_result = new Array(brackets_number[0]/2); for (i = 0; i < brackets_result.length; i++) { brackets_result[i] = new Array(2); } for (var i = 0; i < brackets_number[0]; i++) { if (brackets[i] == "(") { var k =0 while (true) { if (brackets_result[k][0] == undefined) { brackets_result[k][0] = i; break; } k++ } } else { if (brackets[i-1] == "(") { brackets_result[k][1] = i; k++ var j = k } else { while (true) { if (brackets_result[j - 1][1] == undefined) { brackets_result[j - 1][1] = i; break; } j-- } } } } for (var i =0; i < brackets_result.length; i++) { if (brackets_result[i][0] == brackets_number[1]-1) { console.log(brackets_result[i][1]+1) break; } else if(brackets_result[i][1] == brackets_number[1] - 1){ console.log(brackets_result[i][0]+1) } }