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.
__author__ = 'Darren'


def solve():
    import sys
    stdin = sys.stdin if True else open('data')
    from heapq import heappush, heappop

    seq = [c for c in next(stdin).strip()]
    n = len(seq)
    if n % 2 == 1:
        print(-1)
        return

    min_heap = []
    balance, cost = 0, 0
    for i in range(n):
        if seq[i] == '(':
            balance += 1
        elif seq[i] == ')':
            balance -= 1
        else:
            cost_left, cost_right = map(int, next(stdin).split())
            seq[i] = ')'
            balance -= 1
            cost += cost_right
            heappush(min_heap, (cost_left - cost_right, i))

        if balance < 0:
            if len(min_heap) <= 0:
                print(-1)
                return
            diff, index = heappop(min_heap)
            seq[index] = '('
            cost += diff
            balance += 2

    if balance == 0:
        print(cost)
        print(''.join(seq))
    else:
        print(-1)


if __name__ == '__main__':
    solve()
