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. jdk1.7和jdk1.8在接口方面的改动

    1.JDK7及其之前,接口中都是抽象方法,且不能出现static方法 2.接口的变量都是public final static 全局静态常量,无变化 3.接口中可以添加非抽象方法(static),通过 ...

  2. Python入门到进阶必看的权威书籍与网站

    随着人工智能全面爆发,Python[英文单词:蟒蛇],是一款近年来爆红的计算机编程语言.1989年发明,1991年发行,比目前应用最广的Java还要大7岁,有种大器晚成的感觉. 分享之前我还是要推荐下 ...

  3. Python初学者常见错误问题汇总

    1.在客户端和服务端如何传递数组? 答:在客户端和服务端可以使用json进行数据传输.在客户端把数据转换成json字符串,然后使用POST方法发送给服务端. 服务端收集到数据之后,使用json.loa ...

  4. js html生成图片

    我自己分装好的方法,外链自己去下: /** * !!!使用前请导入jq文件!!! 海报生成, 二维码链接生成 */ document.write('<script src="/Publ ...

  5. MAC地址欺骗(原理及实验)

    MAC地址欺骗 MAC地址欺骗(或MAC地址盗用)通常用于突破基于MAC地址的局域网访问控制,例如在交换机上限定只转发源MAC地址修改为某个存在于访问列表中的MAC地址即可突破该访问限制,而且这种修改 ...

  6. zookeeper笔记(一)

    title: zookeeper笔记(一) zookeeper 安装简记 解压文件 $ tar -zxvf zookeeper-3.4.10.tar.gz -C 安装目录 创建软连接(进入安装目录) ...

  7. Spring Security OAuth2 笔记(一)

    关于 refresh_token refresh_token 主要是用来在 access_token 快要过期的时候,对 access_token 进行一个刷新,生成一个新的 access_token ...

  8. java中FutureTask的使用

    文章目录 FutureTask简介 Callable和Runnable的转换 以Runnable运行 java中FutureTask的使用 FutureTask简介 FutureTask是java 5 ...

  9. 为什么LIKELY和UNLIKELY要用两个叹号

    LIKELY和UNLIKELY的一般定义如下: #define LIKELY(x) (__builtin_expect(!!(x),1))#define UNLIKELY(x) (__builtin_ ...

  10. 详解如何使用gulp实现项目在浏览器中的自动刷新

    情况描述: 我们很容易遇到这样一种情况: 我们并不是一开始就规划好了整个项目,比如可能接手别人的项目或者工程已经手动创建好了,现在要想利用gulp来实现浏览器自动刷新,那么如何做呢? 其实非常简单,本 ...