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. Android学习总结(十三) ———— ListView 简单用法

    一.ListView的基本概念 在Android所有常用的原生控件当中,用法最复杂的应该就是ListView了,它专门用于处理那种内容元素很多,手机屏幕无法展示出所有内容的情况.ListView可以使 ...

  2. (十)mybatis之配置(mybatis-config.xml)

    配置  可以从前篇的文章中知道(https://www.cnblogs.com/NYfor2018/p/9093472.html ),要使用mybatis需要以下配置: 1.  mybatis-con ...

  3. ABAP,Java, nodejs和go语言的web server编程

    ABAP and Java see my blog. nodejs 用nodejs现成的express module,几行代码就能写个server出来: var express = require(' ...

  4. Swift REPL入门介绍

    Xcode 6.1 引入了一个新特性用来辅助Swift开发,即Read Eval Print Loop(“读取-求值-输出”循环,简称REPL).熟悉解释型语言的开发者将会对这个命令行环境感到舒适,而 ...

  5. 开源项目: circular-progress-button

    带进度条显示的按钮, 其效果如下所示: 其由三部分动画组成: 初始状态->圆环状态->完成状态. 0. 实现从初始到圆环的简单实现: 继承自button 类, 设置其背景 public c ...

  6. 树形DP 统计树中长度为K的路径数量——Distance in Tree

    一.问题描述 给出一棵n个节点的树,统计树中长度为k的路径的条数(1<=n<=50000 , 1<=k<=500). 二.解题思路 设d[i][k]表示以i为根节点长度为k的路 ...

  7. Linux部署多个tomcat

    Linux部署多个tomcat 1.环境:1.1. Centos 5.01.2.Tomcat 5.5.17 2.需要解决一下几个问题2.1.不同的tomcat启动和关闭监听不同的端口2.2.不同的to ...

  8. shell脚本,一个经典题目。

    [root@localhost wyb]# cat zhuijiu.sh #!/bin/bash #.写一个脚本执行后,输入名字,产生随机数01-99之间的数字. #.如果相同的名字重复输入,抓到的数 ...

  9. Spring 概念及特点 Spring下载地址 控制反转IoC实现原理

    Spring下载地址 http://repo.springsource.org/libs-release-local/org/springframework/spring/ Spring是开源full ...

  10. 控制器生命周期方法(LifeCycle)

    1.init方法: 在init方法中实例化必要的对象(遵从LazyLoad思想) ‍init方法中初始化ViewController本身   2.loadView方法:    当view需要被展示而它 ...