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
()()

import java.util.*;
import java.io.*;

public class d3
{
    public static int[] str;
    public static int n;
    
    public static IntervalTree it1, it2;
    
    public static void main(String[] Args)
    {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        n = s.length();
        str = new int[n];
        it1 = new IntervalTree(n);
        it2 = new IntervalTree(n);
        
        ArrayList<Option> poss = new ArrayList<Option>();
        
        for (int i = 0; i < n; i++)
        {
            if (s.charAt(i) == '(')
            {
                str[i] = 1;
                it1.add(1, 0, i);
                it2.add(1, i, n - 1);
            }
            else if (s.charAt(i) == ')')
            {
                str[i] = -1;
                it1.add(-1, 0, i);
                it2.add(-1, i, n - 1);
            }
            else
            {
                it1.add(-1, 0, i);
                it2.add(1, i, n - 1);
                poss.add(new Option(i,sc.nextInt(), sc.nextInt()));
            }
                
        }
        
        
        if ((n & 1) == 1 || !canValid())
        {
            System.out.println(-1);
            return;
        }
        
        Collections.sort(poss);
        long ans = 0;
        for (Option o : poss)
        {
            set(o.ind, 1);
            str[o.ind] = 1;
            ans += o.a;
            
            if (!canValid())
            {
                set(o.ind, -1);
                str[o.ind] = -1;
                ans += (o.b - o.a);
            }
        }
        
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        out.println(ans);
        for (int i = 0; i < n; i++)
            out.print((str[i] == 1 ? "(" : ")"));
        out.println();
        out.close();
    }
    
    public static void set(int ind, int value)
    {
        // Check if value needs to be unset
        if (str[ind] != 0)
        {
            // it1 stores the front values
            it1.add(-str[ind], 0, ind);
            it2.add(-str[ind], ind, n - 1);
        }
        else
        {
            it1.add(1, 0, ind);
            it2.add(-1, ind, n - 1); 
        }
        it1.add(value, 0, ind);
        it2.add(value, ind, n - 1);
    }
    
    public static int oo = 987654321;
    
    public static class IntervalTree
    {
        int min, max, carry, st, en;
        IntervalTree child1, child2;
        IntervalTree(int n)
        {
            st = 0;
            en = n - 1;
            min = 0;
            max = 0;
            carry = 0;
            if (en == st)
                return;
            int mid = (n - 1) / 2;
            child1 = new IntervalTree(st, mid);
            child2 = new IntervalTree(mid + 1, en);
        }
        IntervalTree(int s, int e)
        {
            st = s;
            en = e;
            min = 0;
            max = 0;
            carry = 0;
            if (en == st)
                return;
            int mid = (s + e) / 2;
            //System.out.println(s +" " +e +" " +mid);
            child1 = new IntervalTree(st, mid);
            child2 = new IntervalTree(mid + 1, en);
        }
        int getMin(int lo, int hi)
        {
            if (lo > en || hi < st)
                return oo;
            if (lo <= st && hi >= en)
                return min + carry;
            prop();
            int p1 = child1.getMin(lo, hi);
            int p2 = child2.getMin(lo, hi);
            if (p1 < p2)
                return p1;
            return p2;
        }
        int getMax(int lo, int hi)
        {
            if (lo > en || hi < st)
                return -oo;
            if (lo <= st && hi >= en)
                return max + carry;
            prop();
            int p1 = child1.getMax(lo, hi);
            int p2 = child2.getMax(lo, hi);
            if (p1 > p2)
                return p1;
            return p2;
        }
        
        void prop()
        {
            child1.push(carry);
            child2.push(carry);
            min += carry;
            max += carry;
            carry = 0;
        }
        void add(int val, int lo, int hi)
        {
            if (lo > en || hi < st)
                return;
            if (lo <= st && hi >= en){
                carry += val;
                return;
            }
            prop();
            child1.add(val, lo, hi);
            child2.add(val, lo, hi);
            min = child1.min + child1.carry;
            if (child2.min + child2.carry < min)
                min = child2.min + child2.carry;
            max = child1.max + child1.carry;
            if (child2.max + child2.carry > max)
                max = child2.max + child2.carry;
        }
        void push(int val)
        {
            carry += val;
        }
    }
    
    public static boolean canValid()
    {
        return (it1.getMax(0, n - 1) <= 0 && it2.getMin(0, n - 1) >= 0);
        
    }
    
    public static class Option implements Comparable<Option>
    {
        int a;
        int b;
        int ind;
        
        Option(int ii, int aa, int bb)
        {
            ind = ii;
            a = aa;
            b = bb;
        }
        
        public int compareTo(Option o)
        {
            return (a - b) - (o.a - o.b);
        }
    }
}
