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×
输出格式
打印第一行中最佳匹配括号序列的成本,并在第二行输出所需的序列。
打印-1,如果没有答案。如果答案不是唯一的,请输出其中的任何一个。
测试用例
测试输入
(??)1 22 8
测试输出
4()()
- CodeForces 3D Least Cost Bracket Sequence
- codeforces 3D Least Cost Bracket Sequence
- Codeforces 3D. Least Cost Bracket Sequence
- CodeForces 3D. Least Cost Bracket Sequence
- CodeForces 3D-Least Cost Bracket Sequence
- CodeForces 3D Least Cost Bracket Sequence
- Codeforces 3D Least Cost Bracket Sequence
- Codeforces 3D Least Cost Bracket Sequence 贪心
- Codeforces 3D Least Cost Bracket Sequence (贪心)
- Codeforces 3D Least Cost Bracket Sequence --- 贪心
- Codeforces Beta Round #3 D. Least Cost Bracket Sequence
- Codeforces 3D Least Cost Bracket Sequence [贪心]
- Codeforces 3D Least Cost Bracket Sequence(贪心)
- Codeforces Beta Round #3 D. Least Cost Bracket Sequence
- CF 3D Least Cost Bracket Sequence
- CF 3D Least Cost Bracket Sequence
- D. Least Cost Bracket Sequence
- CodeForces 3D Least Cost Bracket Sequence (贪心+优先队列)
- 在线选题系统完善篇(PHP)
- NYoj 4 ASCII码排序
- 图像旋转代码
- dojo中的AMD模式开发案例
- HTML的基本结构语法 表格, 框架, 表单
- Codeforces 3D Least Cost Bracket Sequence
- 关于添加WINDOWS静态路由的方法
- 2017携程java后台开发工程师暑期实习生招聘面试经验分享
- Discuz!论坛教程之手机触屏版上传图片质量加强
- HEX
- 为什么CENTOS 7.0开始选择XFS作为默认的文件系统?XFS相比ext有什么优点?
- linux 添加路由
- dojo中的dojo/dom-construct
- apache Tomcat 配置账户管理项目