Least Cost Bracket Sequence(贪心)

Describe

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 a i and b i (1 ≤ a i,  b i ≤ 106), where a i is the cost of replacing the i-th character "?" with an opening bracket, and b i — 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 2
2 8

Output

4
()()

Solution

因为左右括号匹配,我们从第一个字符遍历,定义一个sum=0,‘(’ 就 +1 ,‘)’ 就-1,‘?’ 就做相应的处理(下面再说),我们要保证遍历过程中一直让sum>=0,当然可以使用‘?’,如果遍历到一个地方,即使使用'?'sum仍<0,输出-1.

为了让()完美匹配,他给的’(‘ 和 ‘)’ 就不能改了,所以我们要处理'?',从左向右遍历,如果遇到一个‘?’,将’?‘变为‘)’仍满足sum>=0,那么我们就让‘?’变为‘)’(因为我们一直让左括号不少的嘛),如果?变为)之后,sum<0了,我们就在前面(包括这一个)找一个贡献大的变为)的?,使之变为(。

最后判定sum,sum==0正确输出,sum!=0则只可能是’(‘比‘)’多,输出-1。(‘)’比‘(’多的在上面已经判过了)。

Code

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
using namespace std;
const int maxn=5e4+5;
typedef long long ll;
char s[maxn];
int sum;
ll ans;
priority_queue<pair<int,int> > q;
int main(){
scanf("%s",s);
int len=strlen(s),x,y;
for(int i=0;i<len;++i){
if(s[i]=='(')sum++;
else if(s[i]==')'){
sum--;
if(sum<0){
if(q.empty()){printf("-1\n");return 0;}
else{
int xx=q.top().first,yy=q.top().second;q.pop();
ans-=(ll)xx;
s[yy]='(';
sum+=2;
}
}
}
else if(s[i]=='?'){
scanf("%d%d",&x,&y);
q.push(make_pair(y-x,i));//后面要改就先改y-x值大的,这样贡献才最大
s[i]=')';
ans+=(ll)y;
sum--;
if(sum<0){
if(q.empty()){printf("-1\n");return 0;}
else{
int xx=q.top().first,yy=q.top().second;q.pop();
ans-=(ll)xx;
s[yy]='(';
sum+=2;
}
}
}
}
if(sum!=0)printf("-1\n");
else printf("%lld\n%s",ans,s);
return 0;
}

hzoi

Least Cost Bracket Sequence(贪心)的更多相关文章

  1. CF3D Least Cost Bracket Sequence 贪心

    Least Cost Bracket Sequence CodeForces - 3D 题目描述 This is yet another problem on regular bracket sequ ...

  2. codeforces 3D . Least Cost Bracket Sequence 贪心

    题目链接 给一个字符串, 由( ) 以及? 组成, 将?换成( 或者 ) 组成合法的括号序列, 每一个?换成( 或者 ) 的代价都不相同, 问你最小代价是多少, 如果不能满足输出-1. 弄一个变量nu ...

  3. Codeforces Beta Round #3 D. Least Cost Bracket Sequence 优先队列

    D. Least Cost Bracket Sequence 题目连接: http://www.codeforces.com/contest/3/problem/D Description This ...

  4. CF3D Least Cost Bracket Sequence 题解

    题目 This is yet another problem on regular bracket sequences. A bracket sequence is called regular, i ...

  5. cf3D Least Cost Bracket Sequence

    This is yet another problem on regular bracket sequences. A bracket sequence is called regular, if b ...

  6. 【贪心】【CF3D】 Least Cost Bracket Sequence

    传送门 Description 给一个序列,序列里面会有左括号.问号.右括号.对于一个\(?\)而言,可以将其替换为一个\((\),也可以替换成一个\()\),但是都有相应的代价.问:如何替换使得代价 ...

  7. CF3D Least Cost Bracket Sequence(2500的实力贪心...

    哎,昨天一直在赶课设..没有写 最近听了一些人的建议,停止高级算法的学习,开始刷cf. 目前打算就是白天懒得背电脑的话,系统刷一遍蓝书紫书白书之类的(一直没系统刷过),回宿舍再上机吧. https:/ ...

  8. 【贪心算法】CF3D Least Cost Bracket Sequence

    题目大意 洛谷链接 给一个序列,序列里面会有左括号.问号.右括号.对于一个?而言,可以将其替换为一个(,也可以替换成一个),但是都有相应的代价.问:如何替换使得代价最小.前提是替换之后的序列中,括号是 ...

  9. CodeForces 3 D.Least Cost Bracket Sequence【贪心+优先队列】

    Description 给出一个括号序列,中间有一些问号,将第i个问号换成左括号代价是a[i],换成右括号代价是b[i],问如果用最少的代价将这个括号序列变成一个合法的括号序列 Input 第一行一个 ...

随机推荐

  1. 前端开发--ajax

    使用ajax,他是有两个模块的,一个是客户端,一个是服务端. 客户端负责发送数据,发送数据的方式有两种,一种是GET,另一种是POST. 服务端是用来接收,处理数据和发送请求的数据. 要想使用ajax ...

  2. JavaScript type="text/template"的用法

    JavaScript type="text/template"相当于定义一个模板,如果没有使用html()方法的话,是显示不出来的,我们直接看例子(我是在tp框架的里面写的) &l ...

  3. Apache漏洞利用与安全加固实例分析

    Apache 作为Web应用的载体,一旦出现安全问题,那么运行在其上的Web应用的安全也无法得到保障,所以,研究Apache的漏洞与安全性非常有意义.本文将结合实例来谈谈针对Apache的漏洞利用和安 ...

  4. typeahead自动补全插件的limit参数问题

    遇到的问题很诡异: 后台返回的数据都正确就是显示不正常(有时多有时少),后来发现是typeahead的问题,在1.11版本之后,limit参数从option选项里改到了setdata选项: limit ...

  5. java中Future的使用

    文章目录 创建Future 从Future获取结果 取消Future 多线程环境中运行 java中Future的使用 Future是java 1.5引入的一个interface,可以方便的用于异步结果 ...

  6. 使用spring boot创建fat jar APP

    文章目录 介绍 build和run fat jar和 fat war 更多配置 介绍 在很久很很久以前,我们部署web程序的方式是怎么样的呢?配置好服务器,将自己写的应用程序打包成war包,扔进服务器 ...

  7. js之用IndexOf返回指定字符串的次数

    代码 var Str = "strs,strs,stras,str,strs,strs"; var subStr ="strs" ; var count = 0 ...

  8. LightOJ 1287 Where to Run(期望)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1287 题意:给定一个n个点的无向图(0到n-1),你开始在0.你开始遍历这个图 ...

  9. java 设计模式-责任链

    责任链设计模式,其实就是处理同一个请求的对象连接成一条链,请求的路径经过这条链,符合要求的就处理这个请求,不符合就接着往下面抛出,直道有人处理这条请求. 业务:比如啊,公司个人请假,三天以下就是主管审 ...

  10. js特效:鼠标滑过图片时切换为动图

    效果展示 事前准备 一张普通的静态图+与其对应的gif图. 实现思路 获取图片的src,改变其后缀,使其变成与之对应的gif图片.(很简单有木有= =) 具体实现 编写html代码 <div c ...