https://acs.jxnu.edu.cn/problem/CF3Dhttps://acs.jxnu.edu.cn/problem/CF3D
This is yet another problem on regular bracket sequences.
这是规则括号序列的另一个问题。
A bracket sequence is called regular, if by inserting "+" and "1" into it we get a correct mathematical expression. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. You have a pattern of a bracket sequence that consists of characters "(", ")" and "?". You have to replace each character "?" with a bracket so, that you get a regular bracket sequence.
一个括号序列叫做常规,如果在其中插入“+”和“1”我们将会得到正确的数学表达式。例如,序列"(())()", "()" 和 "(()(()))"是规则的,而 ")(", "(()" 和"(()))("不是。你有一个括号序列的模式由符号"(", ")" 和 "?"组成。你需要用括号来代替“?”,这样你就得到了一个常规的括号序列。
For each character "?" the cost of its replacement with "(" and ")" is given. Among all the possible variants your should choose the cheapest.
对于每个"?",都有变成"("和")"的代价,在所有可能中,你应该选择最便宜的变体。
输入:
The first line contains a non-empty pattern of even length, consisting of characters "(", ")" and "?". Its length doesn't exceed 5·104. Then there follow m lines, where m is the number of characters "?" in the pattern. Each line contains two integer numbers ai and bi (1 ≤ ai, bi ≤ 106), where ai is the cost of replacing the i-th character "?" with an opening bracket, and bi — with a closing one.
第一行包含一个非空且为偶数长度的模式,由 "(", ")" 和 "?"组成。长度不超过5*10^4。接下来有m行,m为模式中"?"的个数。每一行包含两个整数ai和bi(1 ≤ ai, bi ≤ 106),ai是将第i个"?"替换开括号的代价,bi是将第i个"?"替换成关括号的代价。
输出:
Print the cost of the optimal regular bracket sequence in the first line, and the required sequence in the second.
在第一行输出最佳括号序列,在第二行输出序列所需代价。
Print -1, if there is no answer. If the answer is not unique, print any of them.
如果没有答案,输出-1.如果答案不唯一,任意输出。
样例输入:
(??) 1 2 2 8
样例输出:
4 ()()