# CodeForces - 3D

CodeForces - 3D
D. Least Cost Bracket Sequence
time limit per test1 second
memory limit per test64 megabytes
inputstandard input
outputstandard 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.

Examples
input
(??)
1 2
2 8
output
4
()()
AC代码：

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
struct node{
int num, id;
bool operator < (const node & a) const{
return num > a.num;
}
};
char s[N];
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int x, y;
while(~scanf("%s", s)){
int len = strlen(s);
int l = 0, r = 0;
ll ans = 0;
bool flag = false;
priority_queue<node> Q;
for(int i = 0; i < len; i++){
if(s[i] == '(') l++;
else if(s[i] == ')') r++;
else{
scanf("%d%d", &x, &y);
ans += y;
Q.push((node){x - y, i});
r++;
}
if(l < r){
if(Q.empty()) flag = true;
else {
ans += Q.top().num;
s[Q.top().id] = '(';
Q.pop();
l++;
r--;
}
}
}
if(flag || l != r) printf("-1");
else{
printf("%lld\n", ans);
for(int i = 0; i < len; i++){
if(s[i] == '?') printf(")");
else printf("%c", s[i]);
}
}
printf("\n");
}
return 0;
}


## Codeforces 346A Alice and Bob 博弈

http://codeforces.com/problemset/problem/346/A 题意:A和B两个人进行游戏,每人轮流操作,每次从集合(集合元素个数n<=100)取出两个数x,y将|x-y|放入集合中 (|x-y|不存在集合中)不能操作则输 最终游戏结束的标志是无法取出两个数字,他们的差值不在序列中.也就是说,最终状态是一个首项等于公差的等差序列.求出这个等差数列的项数-n,就是游戏进行的回合. 所以我们要先求出首项.设首项为d,接下来就是d+d,d+2d-.后面几项都是首项的倍数

## Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

## Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

## Codeforces Round #408 (Div. 2) B

Description Zane the wizard is going to perform a magic show shuffling the cups. There are n cups, numbered from 1 to n, placed along the x-axis on a table that has m holes on it. More precisely, cup i is on the table at the position x?=?i. The probl