CF BUDDY
← Problems·

1488B · RBS Deletion

1800 · *special, greedy

Problem: A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example:

  • bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)");
  • bracket sequences ")(", "(" and ")" are not.

You are given a string ss, which is an RBS. You can apply any number of operations to this string. Each operation can have one of the following types:

  1. choose some non-empty prefix of ss and remove it from ss, so ss is still an RBS. For example, we can apply this operation as follows: "(())()(())()()" \to "()()" (the first 1010 characters are removed);
  2. choose some contiguous non-empty substring of ss and remove it from ss, so ss is still an RBS. For example, we can apply this operation as follows: "(())()(())()()" \to "(())()()()" (the characters from the 77-th to the 1010-th are removed).

The operation 22 can be applied at most kk times. Calculate the maximum number of operations you can apply until ss becomes empty.

Input Format: The first line contains one integer tt (1t1051 \le t \le 10^5) — the number of test cases.

Each test case is described by two lines. The first line contains two integers nn and kk (2n21052 \le n \le 2 \cdot 10^5; 1kn1 \le k \le n; nn is even) — the length of ss and the maximum number of operations of type 22 you can apply.

The second line contains a string ss of nn characters '(' and ')'. This string is an RBS.

The sum of nn over all test cases doesn't exceed 21052 \cdot 10^5.

Output Format: For each test case, print one integer — the maximum number of operations you can apply.

Sample Cases

Case 1

Input

3
12 2
(()())((()))
6 3
()()()
8 1
(((())))

Output

4
3
2

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback