Codeforces 3D Least Cost Bracket Sequence

来源:互联网 发布:软件项目阶段性总结 编辑:程序博客网 时间:2024/05/20 17:59

English

Desc

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.

For each character “?” the cost of its replacement with “(” and “)” is given. Among all the possible variants your should choose the cheapest.

Input

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.

Output

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.

Examples

input

(??)1 22 8

output

4()()

中文

简介

这是括号匹配的另一个问题。

例如,“ (())() ”,“ () ”和“ (()(())) ”是匹配的,而“ ( ”)“ (() ”和“ (())) “不是匹配的,你有一个由” ( “,” ) “和” ? “ 字符组成的括号序列的模式,你必须用括号替换每个字符” ? “,以便得到一个匹配的括号序列。

对于每个字符“ ? ”,给出用“ ( ”和“ ) ” 替换的成本。在所有可能的变种中,选择最便宜的一种方案。

输入格式

第一行包含一个非空格式的均匀长度,由字符“ ( ”,“ ) ”和“ ? ”组成。其长度不超过5×104。那么跟随m行,其中m是模式中的’?’的个数。每行包含两个整数一个ai和bi(1≤  ai,   bi  ≤106),其中,ai是更换的成本,ai是更换为’(‘的成本,bi是更换为’)’的成本

输出格式

打印第一行中最佳匹配括号序列的成本,并在第二行输出所需的序列。
打印-1,如果没有答案。如果答案不是唯一的,请输出其中的任何一个。

测试用例

测试输入

(??)1 22 8

测试输出

4()()
0 0
原创粉丝点击