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.



expr = input()
N = len(expr)
new_expr = [c for c in expr] 
n = expr.count("?")

C = []

for _ in range(n):
    c = input().split(' ')
    C.append((int(c[0]), int(c[1]), abs(int(c[0]) - int(c[1]))))

k = 0
b = 0
posb = []
posi = []


def check():
    a = expr.count('(')
    b = expr.count(')')
    return (a - b - N <= 0) and  (a - b + N >= 0)
    
def compute_cost():
    cost = 0
    for i, b in zip(posi, posb):
        print(i, new_expr[i])
        if new_expr[i] == "(":
            cost += C[b][0]
        else:
            cost += C[b][1]
    return cost
    

def find_correction1():
    global k
    p = [(C[b][2], i) for i, b in zip(posi ,posb) if new_expr[i] == ")"]
    new_expr[min(p)[1]] = "("
    k = k + 2

def find_correction2():
    global k
    p = [(C[b][2], i) for i, b in zip(posi, posb) if new_expr[i] == "("]
    new_expr[min(p)[1]] = ")"
    k = k - 2
    

for i in range(N):
    if k < 0:
        print("need correction")
        find_correction1()
    if expr[i] == '(':
        k += 1
    elif expr[i] == ')':
        k -= 1
    else:
        posi.append(i)
        if C[b][0] < C[b][1]:
            new_expr[i] = "("
            k += 1
        else:
            new_expr[i] = ")"
            k -= 1
        posb.append(b)
        b += 1

while k != 0:
    find_correction2()

if check():
    print(compute_cost())
    print("".join(new_expr))
else:
    print(- 1)
    print()
