题目链接

D. Least Cost Bracket Sequence
time limit per test

1 second

memory limit per test

64 megabytes

input

standard input

output

standard output

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.

Sample test(s)
input
(??)
1 2
2 8
output
4
()()


 Accepted Code:

 from heapq import *

 s = list(raw_input());
ans, heap = 0, [];
b = 0
for i, v in enumerate(s):
if v == '(':
b += 1
elif v == ')':
b -= 1
else :
b -= 1;
x, y = map(int, raw_input().split());
ans += y;
s[i] = ')'
heappush(heap, (x-y, i));
if b < 0:
if (not heap):
print -1
quit()
b += 2;
x, y = heappop(heap);
ans += x;
s[y] = '(';
if b:
print '-1';
else :
print ans
print ''.join(s)

Codeforces 3D的更多相关文章

  1. 【Codeforces 3D】Least Cost Bracket Sequence

    Codeforces 3 D 题意:有一个括号序列,其中一些位置是问号,把第\(i\)个问号改成(需要\(a_i\)的代价,把它改成)需要\(b_i\)的代价. 问使得这个括号序列成立所需要的最小代价 ...

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

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

  3. codeforces 3D (非原创)

    D. Least Cost Bracket Sequence time limit per test 1 second memory limit per test 64 megabytes input ...

  4. XTU1266:Parentheses(贪心+优先队列)

    传送门 题意 从左到右有n个连续的组,每一组有Li个括号,要么全是左括号,要么全是右括号,以及该组的每一个左括号翻成右括号, 或者右括号翻成左括号的花费Di.可以对这n个组的括号进行翻转,每一个括号都 ...

  5. CF3D Least Cost Bracket Sequence 贪心

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

  6. CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)

    E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...

  7. Codeforces 681C. Heap Operations 优先队列

    C. Heap Operations time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  8. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表

    E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...

  9. Codeforces - 346A - Alice and Bob - 简单数论

    http://codeforces.com/problemset/problem/346/A 观察了一下,猜测和他们的最大公因数有关,除以最大公因数前后结果是不会变的. 那么怎么证明一定是有n轮呢?我 ...

随机推荐

  1. Java MySQL 批量查询数据,每次查询10条

    因为 数据量比较多, 比如每次 /** * 批量查询 * @param sourList * @param batchCount * @param userMapper * @return */ pu ...

  2. C开发系列-函数

    概述 任何一个C语言程序都是有一个或多个程序段(小程序构成).每个程序都有自己的功能,我们一般称这些程序段为"函数". 函数的执行过程 #import <Foundation ...

  3. Windows API 第15篇 GetVolumeInformation 获取磁盘卷(驱动器)信息

    先看定义:BOOL GetVolumeInformation(    [IN]  LPCTSTR lpRootPathName,           // root directory  卷所在的根目 ...

  4. 新浪新闻API

    新浪新闻API ustcmio 关注 2017.01.15 20:44* 字数 536 阅读 2479评论 2喜欢 7 新浪新闻的API:1.访问手机新浪网https://sina.cn/?from= ...

  5. Nginx简介与基础配置

    何为Nginx? Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.最初是为了解决C10k的问题,由Igor ...

  6. Odoo中Qweb使用入门

    参考 可参考官网例子https://doc.odoo.com/trunk/web/qweb/或 http://thierry-godin.developpez.com/openerp/tutorial ...

  7. vue 学习 二

    动画 <transition name="fade"> <p v-if="show">hello</p> </tran ...

  8. NMS 和 Soft-NMS

    转自https://zhuanlan.zhihu.com/p/42018282 一 NMS NMS算法的大致思想:对于有重叠的候选框:若大于规定阈值(某一提前设定的置信度)则删除,低于阈值的保留.对于 ...

  9. R语言画图教程之盒形图

    R语言画图教程之盒形图 我们之前有分享过一系列的R语言画图代码(PCA图.Pathway图.火山图.RDA图.热图),今天再来补充一个盒形图(箱形图)的代码. 以下代码只是示例,不能直接搬来用哦,注意 ...

  10. OpenCASCADE动画功能

    OpenCASCADE动画功能 eryar@163.com 1.Introduction OpenCASCADE提供了类AIS_Animation等来实现简单的动画功能. 从其类图可以看出,动画功能有 ...