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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class D3 implements Runnable {

    public static void main(String[] args) {
        new Thread(new D3()).start();
    }

    public void run() {
        try {
            solve();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");

    private void solve() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        String bracketsStr = nextToken();
        Place[] places = new Place[bracketsStr.length()];
        for (int i = 0; i < bracketsStr.length(); i++) {
            Place place = new Place(bracketsStr.charAt(i));
            if (place.originalType == PlaceType.ANY) {
                place.openCost = nextInt();
                place.closeCost = nextInt();
            }
            places[i] = place;
        }
        br.close();

        boolean res = subsolve(places);
        PrintWriter pw = new PrintWriter(System.out);
        if (res) {
            long cost = 0;
            StringBuilder sb = new StringBuilder(places.length);
            for (Place place : places) {
                sb.append(place.nowType == PlaceType.OPEN ? '(' : ')');
                if (place.nowType == PlaceType.OPEN) {
                    cost += place.openCost;
                } else {
                    cost += place.closeCost;
                }
            }
            pw.println(cost);
            pw.println(sb);
        } else {
            pw.println(-1);
        }
        pw.close();
    }

    private boolean subsolve(Place[] places) {
        boolean valid = validate(places);
        if (!valid) {
            return false;
        }

        int[] minHeights = new int[places.length];
        minHeights[places.length - 1] = places[places.length - 1].height;
        for (int i = places.length - 2; i >= 0; i--) {
            minHeights[i] = Math.min(minHeights[i + 1], places[i].height);
        }

        PriorityQueue<Place> closeBracketQueue = new PriorityQueue<Place>(10, new Comparator<Place>() {
            public int compare(Place p1, Place p2) {
                return p1.closeProfit() - p2.closeProfit();
            }
        });

        for (int i = 0; i < places.length; i++) {
            Place place = places[i];
            if (place.originalType != PlaceType.ANY) {
                continue;
            }
            int minHeight = minHeights[i];
            int reverses = closeBracketQueue.size();
            if (minHeight - 2 * reverses <= 1) {
                Place leastProfitPlace = closeBracketQueue.peek();
                if (leastProfitPlace != null && leastProfitPlace.closeProfit() < place.closeProfit()) {
                    leastProfitPlace.nowType = PlaceType.OPEN;
                    place.nowType = PlaceType.CLOSE;
                    closeBracketQueue.poll();
                    closeBracketQueue.add(place);
                }
            } else {
                place.nowType = PlaceType.CLOSE;
                closeBracketQueue.add(place);
            }
        }
        return places[places.length - 1].height - 2 * closeBracketQueue.size() == 0;
    }

    private boolean validate(Place[] places) {
        int height = 0;
        for (Place place : places) {
            height += place.nowType == PlaceType.OPEN ? 1 : -1;
            place.height = height;
            if (height < 0) {
                return false;
            }
        }
        return true;
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    String nextToken() throws IOException {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

    enum PlaceType {
        OPEN, CLOSE, ANY
    }

    static class Place {
        final PlaceType originalType;
        PlaceType nowType;
        int openCost;
        int closeCost;
        int height;

        Place(char ch) {
            if (ch == '(') {
                this.originalType = PlaceType.OPEN;
            } else if (ch == ')') {
                this.originalType = PlaceType.CLOSE;
            } else {
                this.originalType = PlaceType.ANY;
            }
            if (this.originalType == PlaceType.ANY) {
                this.nowType = PlaceType.OPEN;
            } else {
                this.nowType = this.originalType;
            }
        }

        int closeProfit() {
            return this.openCost - this.closeCost;
        }
    }

}
