RBS Deletion
Contest: Kotlin Heroes: Episode 6
Difficulty rating: 1800
Tags: *special, greedy

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 $$$s$$$, 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 $$$s$$$ and remove it from $$$s$$$, so $$$s$$$ is still an RBS. For example, we can apply this operation as follows: "(())()(())()()" $$$\to$$$ "()()" (the first $$$10$$$ characters are removed);
2. choose some contiguous non-empty substring of $$$s$$$ and remove it from $$$s$$$, so $$$s$$$ is still an RBS. For example, we can apply this operation as follows: "(())()(())()()" $$$\to$$$ "(())()()()" (the characters from the $$$7$$$-th to the $$$10$$$-th are removed).

The operation $$$2$$$ can be applied at most $$$k$$$ times. Calculate the maximum number of operations you can apply until $$$s$$$ becomes empty.

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

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

The second line contains a string $$$s$$$ of $$$n$$$ characters '(' and ')'. This string is an RBS.

The sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

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

Constraints:
Time limit: 3.0 s
Memory limit: 512.0 MB

Example input:
3
12 2
(()())((()))
6 3
()()()
8 1
(((())))

Example output:
4
3
2

Editorial:
It's quite easy to see that the answer cannot exceed $$$\frac{n}{2}$$$ (each action removes at least two characters).
Let's build the balance array for our string $$$s$$$ ($$$bal_i$$$ is the difference between the count of opening and closing brackets among the first $$$i$$$ characters). The first operation allows us to remove some prefix with $$$bal_i = 0$$$, so if we were allowed to use only the operations of the first type, the answer would be equal to the number of non-empty prefixes with $$$bal_i = 0$$$ (let's call it $$$c$$$).
If we want to apply a second operation, let's find a way to do it so the number of prefixes with $$$bal_i = 0$$$ doesn't decrease. We can always pair of neighbouring brackets (), and removing it still makes $$$s$$$ an RBS. So, let's remove some pair of neighbouring brackets that doesn't decrease the number of $$$bal_i = 0$$$, until the string becomes ()()()()..., where each first operation can remove just $$$2$$$ characters, or until we run out of operations of type $$$2$$$.
So, the answer to our problem can be calculated as $$$\min(\frac{n}{2}, k + c)$$$.