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. HTML iframe框架

    iframe 作用: 就是在一个网页插入一个小窗口   窗口里面也是一个网页 <a href="http://www.baidu.com" target="da1& ...

  2. codevs 1487 大批整数排序(水题日常)

     时间限制: 3 s  空间限制: 16000 KB  题目等级 : 黄金 Gold 题目描述 Description !!!CodeVS开发者有话说: codevs自从换了评测机,新评测机的内存计算 ...

  3. Jquery 错误提示插件

    这是一个简单的输入框错误提示插件,可拓展! .jq-error{ font-size:12px; min-width:150px; width:auto; max-width:350px; line- ...

  4. leecode 旋转数组

    描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋 ...

  5. Web项目之Django基础

    Django目录: python项目Django(web服务) python项目Django(HTTP协议) python项目Django(Django的安装与使用) python项目Django(U ...

  6. k8s学习目录

    目录 K8S基础部分 基础部分 5 秒创建 k8s 集群[转] k8s 核心功能[转] k8s 重要概念[转] 部署 k8s Cluster(上)[转] 部署 k8s Cluster(下)[转] Ku ...

  7. kitti raw data development kit的使用

    run_demoVelodyne.m使用:http://blog.csdn.net/qq_33801763/article/details/78959205   https://www.cnblogs ...

  8. Java MiniUi datagrid加载数据时,如果使用virtualScroll="false",数据多一点可能就会加载不出来

    datagrid的值为 virtualScroll="true" 问题解决.

  9. nyoj-47-过河问题|POJ-1700-Crossing River

    http://acm.nyist.net/JudgeOnline/problem.php?pid=47 http://poj.org/problem?id=1700 解题思路:求最少需要多少时间才能都 ...

  10. java在线聊天项目0.8版 实现把服务端接收到的信息返回给每一个客户端窗口中显示功能

    迭代器的方式会产生锁定 服务器端增加发送给每个客户端已收到信息的功能 所以当获取到一个socket,并打开它的线程进行循环接收客户端发来信息时,我们把这个内部类的线程Client保存到集合List&l ...