Codeforces Round 1051 (Div. 2)
E
Make Good
Codeforces
2400
原题
← D2F →
翻译中…

You are given a bracket string $s$ of length $n$. You can apply the following operations arbitrary number of times (possibly, zero) and in any order:

翻译中…
  • Choose an index $1 \leq i \lt |s|$ such that $s_i = s_{i+1} = \texttt{(}$, and replace both $s_i$ and $s_{i+1}$ with $\texttt{)}$.
  • Choose an index $1 \leq i \lt |s|$ such that $s_i = s_{i+1} = \texttt{)}$, and replace both $s_i$ and $s_{i+1}$ with $\texttt{(}$.
翻译中…

Find a regular bracket sequence$^{\text{∗}}$ $t$ which can be obtained from $s$ by applying the operations described above, or print $-1$ if there is no such $t$.

翻译中…

$^{\text{∗}}$A regular bracket sequence 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: the bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"); the bracket sequences ")(", "(" and ")" 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 contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the string.

翻译中…

The second line contains a single string $s$ — a sequence of the characters ( and ) of length $n$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case output a regular bracket string $t$ which can be obtained from $s$ or print $-1$ if there is no such $t$.

翻译中…

Note

翻译中…

In the first testcase, the string "()()" is already a regular bracket sequence. No changes needed.

翻译中…

In the second and third testcases it's impossible to reach a regular bracket string from $s$.

翻译中…

In the fourth testcase we can do the following:

翻译中…

"))))))))" $\xrightarrow{i = 1}$ "(())))))"

翻译中…

"(())))))" $\xrightarrow{i = 5}$ "(())(())".

翻译中…

样例

Input #1
5
4
()()
6
((())(
10
))(())())(
8
))))))))
1
(
Output #1
()()
-1
-1
(())(())
-1
InputCopy OutputCopy
题目信息
题号
2143E
时间限制
2 s
内存限制
512 MB
标签
构造
贪心
数学
字符串

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