WorldQuant Round 1 (Codeforces Round 1128, Div. 1 + Div. 2)
E
What Does the Cow Say?
Codeforces
1800
原题
← D2F1 →
翻译中…

Oh, Moo, Moo, Moo, I speak that normally!— Bessie the Cow

翻译中…

Bessie gives you a regular bracket sequence (RBS)$^{\text{∗}}$ of length $n$ and an integer $k$.

翻译中…

In one operation, you can do the following:

翻译中…
  • Choose an index $i$ ($1 \le i \lt n$) such that $s_i s_{i+1} = \mathtt{()}$.
  • Let $s'$ denote the string obtained after removing these two characters from $s$.
  • Set $s := \mathtt{(} + s' + \mathtt{)}$.
翻译中…

You may perform this operation at most $k$ times (possibly zero). It can be shown that $s$ remains an RBS after every such operation.

翻译中…

The score of an RBS is defined as the maximum number of consecutive opening brackets '$\texttt{(}$' in the string. For example, the score of the string "$\texttt{(())((())())}$" is $3$.

翻译中…

Your task is to find the maximum possible score you can obtain by performing at most $k$ operations on the given string.

翻译中…

$^{\text{∗}}$A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting the characters $1$ and $+$ between the original characters of the sequence. For example: bracket sequences $\texttt{()()}$ and $\texttt{(())}$ are regular (the resulting expressions are: $\texttt{(1)+(1)}$ and $\texttt{((1+1)+1)}$); bracket sequences $\texttt{)(}$, $\texttt{(}$ and $\texttt{)}$ are not.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $k$ ($2\le n \le 4 \cdot 10^5$, $0 \le k \le \frac{n}{2}$) — the length of the RBS and the maximum number of operations you may perform.

翻译中…

The second line of each test case contains a string $s$ of length $n$ consisting of characters '$\texttt{(}$' and '$\texttt{)}$' — the given RBS.

翻译中…

It is guaranteed that $s$ is a regular bracket sequence, and that the sum of $n$ over all test cases does not exceed $4 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, print a single integer — the maximum score you can obtain by performing at most $k$ operations.

翻译中…

Note

翻译中…

In the first test case, we cannot perform any operations. The maximum number of consecutive opening brackets is $1$.

翻译中…

In the third test case, we can choose index $i = 1$, performing the operation gives us $s = $ "$\texttt{(()())}$". Then, choosing index $i = 4$ gives us $s = $ "$\texttt{((()))}$". We used $2$ operations and got a score of $3$, which is the maximum possible score.

翻译中…

In the seventh test case, we can choose index $i = 6$, performing the operation gives us $s = $ "$\texttt{(()(((((()))))))}$", which has a score of $6$. It can be shown that $6$ is the maximum possible score achievable.

翻译中…

样例

Input #1
9
2 0
()
4 1
(())
6 2
()()()
8 1
()((()))
8 3
(()())()
12 4
((()())())()
16 1
()(((()((())))))
22 1
((((()()(())((()))))))
28 0
(((()(((())()((())))())))())
Output #1
1
2
3
4
4
6
6
6
4
InputCopy OutputCopy
题目信息
题号
2261E
时间限制
2 s
内存限制
256 MB
标签
二分
数据结构
DFS 及类似
贪心
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。