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.io.*;
import java.text.*;
import java.math.*;
import java.util.*;

public class Main implements Runnable {

    String s;
    char[] ss;
    long ans;
    int[] x, y;

    public void smart() {
        int N = s.length();

        int m = 0;
        for (int i = 0; i < N; i++)
            if (s.charAt(i) == '?')
                m++;
        pair[] pairs = new pair[m];
        pair[] rev = new pair[N];
        m = 0;

        int balance = 0;
        for (int i = 0; i < N; i++)
            if (s.charAt(i) == '?') {
                pair p = new pair(i, x[m], y[m]);
                pairs[m++] = p;
                rev[i] = p;
            } else if (s.charAt(i) == ')')
                balance--;
            else
                balance++;
        long cursum = 0;
        Arrays.sort(pairs);
        int right = (m + balance) / 2, left = m - right;
        if (right<0||left<0)
        {
            ans=-1;
            return;
        }
        for (int i = 0; i < left; i++) {
            pairs[i].state = 0;
            cursum += pairs[i].a;
        }
        for (int i = left; i < m; i++) {
            pairs[i].state = 1;
            cursum += pairs[i].b;
        }
        PriorityQueue<pair> q1 = new PriorityQueue<pair>(), q2 = new PriorityQueue<pair>(10, new pair_comp_2());
        for (int i = 0; i < left; i++)
            q2.add(pairs[i]);

        balance = 0;
        for (int i = 0; i < N; i++) {
            a: {
                if (s.charAt(i) == '(') {
                    balance++;
                    break a;
                }
                if (s.charAt(i) == ')') {
                    balance--;
                    break a;
                }
                if (rev[i].state == 1) {
                    balance--;
                    q1.add(rev[i]);
                } else
                    balance++;
            }
            if (balance < 0) {
                while (q2.size() > 0 && q2.peek().x <= i)
                    q2.poll();
                if (q1.size() == 0 || q2.size() == 0) {
                    ans = -1;
                    return;
                }
                pair p = q1.poll(), q = q2.poll();
                p.state = 0;
                cursum -= p.b;
                cursum += p.a;

                q.state = 1;
                cursum -= q.a;
                cursum += q.b;
                balance += 2;
            }
        }
        ans = cursum;
        ss = s.toCharArray();
        for (int i = 0; i < N; i++)
            if (ss[i] == '?')
                ss[i] = rev[i].state == 1 ? ')' : '(';
    }

    public void solve() throws Exception {
        s = readword();
        int m = 0;
        for (int i = 0; i < s.length(); i++)
            if (s.charAt(i) == '?')
                m++;
        x = new int[m];
        y = new int[m];
        for (int i = 0; i < m; i++) {
            x[i] = iread();
            y[i] = iread();
        }

        smart();
        out.write(ans + "\n");
        if (ans >= 0) {
            out.write(ss);
            out.write("\n");
        }
    }

    void dummy() {
        int N = s.length();
        int m = 0;
        for (int i = 0; i < s.length(); i++)
            if (s.charAt(i) == '?')
                m++;
        ans = Integer.MAX_VALUE;
        cycle: for (int mask = 0; mask < 1 << m; mask++) {
            int balance = 0;
            int cursum = 0;
            int j = 0;
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == '(')
                    balance++;
                else if (s.charAt(i) == ')')
                    balance--;
                else {
                    if ((mask & (1 << j)) == 0) {
                        cursum += x[j];
                        balance++;
                    } else {
                        cursum += y[j];
                        balance--;
                    }
                    j++;
                }
                if (balance < 0)
                    continue cycle;
            }
            if (balance == 0) {
                ans = Math.min(ans, cursum);
                ss = s.toCharArray();
                j = 0;
                for (int i = 0; i < N; i++)
                    if (ss[i] == '?') {
                        ss[i] = (mask & (1 << j)) == 0 ? '(' : ')';
                        j++;
                    }
            }
        }
        if (ans == Integer.MAX_VALUE)
            ans = -1;
    }

    void test() {
        Random r = new Random(32167);
        while (true) {
            int N = 10;
            s = "";
            for (int i = 0; i < N; i++)
                s += "?";
            x = new int[N];
            y = new int[N];
            for (int i = 0; i < N; i++) {
                x[i] = r.nextInt(10);
                y[i] = r.nextInt(10);
            }
            dummy();
            long ans1 = ans;
            smart();
            long ans2 = ans;
            if (ans1 != ans2) {
                System.out.println("FAIL\n");
                System.out.println("s="+s);
                for (int i=0;i<N; i++)
                    System.out.println(x[i]+" "+y[i]);
                System.out.println("ans1="+ans1+" ans2="+ans2); 
                break;
            }
            System.out.println("DONE");
        }
    }

    class pair implements Comparable<pair> {
        int a, b, x;
        int state;

        public pair(int x, int a, int b) {
            this.x = x;
            this.a = a;
            this.b = b;
            state = 0;
        }

        @Override
        public int compareTo(pair o) {
            return (a - b) - (o.a - o.b);
        }
    }

    class pair_comp_2 implements Comparator<pair> {

        @Override
        public int compare(pair o1, pair o2) {
            return (o1.b - o1.a) - (o2.b - o2.a);
        }

    }

    public void run() {
        try {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new BufferedWriter(new OutputStreamWriter(System.out));
//          test();
            solve();
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public int iread() throws Exception {
        return Integer.parseInt(readword());
    }

    public double dread() throws Exception {
        return Double.parseDouble(readword());
    }

    public long lread() throws Exception {
        return Long.parseLong(readword());
    }

    BufferedReader in;

    BufferedWriter out;

    public String readword() throws IOException {
        StringBuilder b = new StringBuilder();
        int c;
        c = in.read();
        while (c >= 0 && c <= ' ')
            c = in.read();
        if (c < 0)
            return "";
        while (c > ' ') {
            b.append((char) c);
            c = in.read();
        }
        return b.toString();
    }

    public static void main(String[] args) {
        try {
            Locale.setDefault(Locale.US);
        } catch (Exception e) {

        }
        new Thread(new Main()).start();
    }
}