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.
The first line contains a non-empty pattern of even length, consisting of characters "(", ")" and "?". Its length doesn't exceed 5·10<sup class="upper-index">4</sup>. Then there follow *m* lines, where *m* is the number of characters "?" in the pattern. Each line contains two integer numbers *a*<sub class="lower-index">*i*</sub> and *b*<sub class="lower-index">*i*</sub> (1 ≤ *a*<sub class="lower-index">*i*</sub>,  *b*<sub class="lower-index">*i*</sub> ≤ 10<sup class="upper-index">6</sup>), where *a*<sub class="lower-index">*i*</sub> is the cost of replacing the *i*-th character "?" with an opening bracket, and *b*<sub class="lower-index">*i*</sub> — with a closing one.
#3D - Least cost brackets sequence

#1st line of input - pattern
pattern = list(input())
#we keep track of how many "?" marks we've got
n = pattern.count("?")
# a - list of all the costs of replacing the open parantheses
# b - list of all the costs of replacing the close parantheses
a = []
b = []
#Then we take the next lines of input:
#the i-th line contains info about the cost of replacing
#the i-th "?" mark with an open and a close paranthesis
for i in range(n):
    o, c = map(int, input().split())
    a.append(o)
    b.append(c)
 
q = 0
#We keep track of how many "(" we have to balance
balance = 0
# and the cost for the i-th mark
cost = 0
# we also compare the cost of replacing the i-th mark
# with either an "(" or a ")"
delta_costs = []
#Iterating through the pattern, we start the accounting
#The code is kinda self-explaining, since it's almost like
# short sentences
for i in range(len(pattern)):
    if pattern[i] == "(":
        balance += 1
    elif pattern[i] == ")": # ")" balances an "("
        balance -= 1
    elif pattern[i] == "?":
        pattern[i] = ")"
        cost += b[q]
        delta_costs.append((a[q]-b[q], i))
        q += 1
        balance -= 1
#The case when we don't have an answer    
    if balance < 0:
        # print("Here")
        if len(delta_costs) == 0:
            cost = -1
            break
        delta_costs.sort()
        d, j = delta_costs.pop(0)
        cost += d
        pattern[j] = "("
        balance += 2
#We print the final optimal cost 
print(cost)
#Also, if there's an answer, we print the pattern
if cost != -1:
    print ("".join(pattern))