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.

Example

Input
(??)
1 2
2 8
Output
4
()()

给一个包含'(' ')' '?'的括号序列,其中'?'需要替换成'(' ')'使得它变成一个合法的括号序列,每个'?'都有一个变成‘(’ ')'的费用,要使得费用最小

不妨先把所有'?'用')'替换,然后再考虑把其中的一些')'改成'('。这样改回'('的费用就是ai-bi

如果'?'改成了')'导致前k个字符不是合法的括号序列了,就把前k个字符中找个ai-bi最小的改掉,这个维护个堆就好

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
priority_queue<pa,vector<pa>,greater<pa> >q;
char s[];
int pos[];
int l[],r[];
int n,p;
LL ans;
int main()
{
scanf("%s",s+);n=strlen(s+);
for (int i=;i<=n;i++)
{
if (s[i]=='(')p++;
else if (s[i]==')')
{
p--;
if (p<)
{
if (q.empty()){puts("-1");return ;}
p+=;
pa now=q.top();q.pop();
ans+=now.first;s[now.second]='(';
}
}else if (s[i]=='?')
{
int l=read(),r=read();
s[i]=')';p--;
ans+=r;q.push(mkp(l-r,i));
if (p<)
{
if (q.empty()){puts("-1");return ;}
p+=;
pa now=q.top();q.pop();
ans+=now.first;s[now.second]='(';
}
}
}
while (p<)
{
p+=;
pa now=q.top();q.pop();
ans+=now.first;s[now.second]='(';
}
p=;
for (int i=;i<=n;i++)
{
if (s[i]=='(')p++;else p--;
if (p<){puts("-1");return ;}
}
if (p!=){puts("-1");return ;}
printf("%lld\n",ans);
puts(s+);
}

cf 3D

cf3D 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. CF3D Least Cost Bracket Sequence 题解

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

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

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

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

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

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

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

  6. Least Cost Bracket Sequence(贪心)

    Least Cost Bracket Sequence(贪心) Describe This is yet another problem on regular bracket sequences. A ...

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

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

  8. 3 D. Least Cost Bracket Sequence

    题目大意: 这是一个规则的字符括号序列.一个括号序列是规则的,那么在序列里面插入‘+’ 和 ‘1’ 会得到一个正确的数学表达式. 合法:(())(), (),(()(())) 不合法:)(,((),( ...

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

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

随机推荐

  1. sql server 2008怎么设置不允许windows身份验证

  2. UWP中获取Encoding.Default

    Encoding.GetEncoding(0); 即可

  3. Example of how to implement a view-based source list (NSOutlineView) using Cocoa Bindings

    You tagged this with the cocoa-bindings tag, so I assume you mean "with bindings." I whipp ...

  4. canvas 在视频中的用法

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. myna代码

    https://github.com/TalkingData/Myna/tree/master/Dataset https://github.com/TalkingData/Myna

  6. 欧拉函数φ(x)简要介绍及c++实现

    我还是很喜欢数论,从此吃喝不问,就此沉沦. 欧拉函数φ(x)的值为在[1,x)的区间内与x互质的数的个数 通式:    其中p1, p2……pn为x的所有质因数,x是不为0的整数.φ(1)=1. 注意 ...

  7. Mysql 5.7在Linux上部署及远程访问

    序言:最近要和伙伴一起组队,做.NET Core项目.所以自己就租了一个阿里云服务器,并且装了Linux和MySQL.这里面我的Linux是CentOs 7. 第一步 添加Mysql Yum库 这里面 ...

  8. Javaweb开发之路

    本文作者:DavidLin 欢迎转载,但请保留文章原始出处→_→ 本文地址:http://www.cnblogs.com/univeryinli/p/programming-skill-yinli.h ...

  9. docker系列之基础命令-1

    1.docker基础命令 docker images 显示镜像列表 docker ps 显示容器列表 docker run IMAGE_ID 指定镜像, 运行一个容器 docker start/sto ...

  10. PHP发送邮件标题乱码的解决

    遇到问题:PHPMailer发送邮件时中文乱码,本来我的系统都是英文内容的,后来需求变化需要在标题中添加中文,但是在使用安卓自带邮件工具收取是出现乱码,而使用QQ邮箱查看确实正常的. 解决方法: 先用 ...