1.26Least Cost Bracket Sequence最低成本括号序列

https://acs.jxnu.edu.cn/problem/CF3Dicon-default.png?t=M0H8https://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
()()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值