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
()()
#include <bits/stdc++.h>
using namespace std;
const int MX = 5e4 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n;
pair<int, int> MIN[MX << 2];
int ps[MX << 2], col[MX << 2];
int MIN_init[MX], ps_init[MX];
int mark_pos[MX];
char S[MX], debug = 0;
void push_up(int rt) {
  MIN[rt] = min(MIN[rt << 1], MIN[rt << 1 | 1]);
  ps[rt] = min(ps[rt << 1], ps[rt << 1 | 1]);
  if (debug && rt == 1) {
    printf("{%d,%d}\n", ps[rt << 1], ps[rt << 1 | 1]);
  }
}
void seg_updone(int d, int rt) {
  col[rt] += d;
  ps[rt] += d;
}
void push_down(int rt) {
  if (col[rt]) {
    seg_updone(col[rt], rt << 1);
    seg_updone(col[rt], rt << 1 | 1);
    col[rt] = 0;
  }
}
void seg_add(int L, int R, int d, int l, int r, int rt) {
  if (L <= l && r <= R) {
    seg_updone(d, rt);
    return;
  }
  int m = (l + r) >> 1;
  push_down(rt);
  if (L <= m) seg_add(L, R, d, l, m, rt << 1);
  if (R > m) seg_add(L, R, d, m + 1, r, rt << 1 | 1);
  push_up(rt);
}
int seg_pos(int l, int r, int rt) {
  if (l == r) {
    return l;
  }
  int m = (l + r) >> 1;
  push_down(rt);
  if (ps[rt << 1] < 0) return seg_pos(l, m, rt << 1);
  return seg_pos(m + 1, r, rt << 1 | 1);
}
pair<int, int> min_query(int L, int R, int l, int r, int rt) {
  if (L <= l && r <= R) {
    return MIN[rt];
  }
  int m = (l + r) >> 1;
  push_down(rt);
  pair<int, int> ret(INF, INF);
  if (L <= m) ret = min(ret, min_query(L, R, l, m, rt << 1));
  if (R > m) ret = min(ret, min_query(L, R, m + 1, r, rt << 1 | 1));
  return ret;
}
void min_update(int p, int l, int r, int rt) {
  if (l == r) {
    MIN[rt].first = INF;
    return;
  }
  int m = (l + r) >> 1;
  push_down(rt);
  if (p <= m)
    min_update(p, l, m, rt << 1);
  else
    min_update(p, m + 1, r, rt << 1 | 1);
  push_up(rt);
}
void build(int l, int r, int rt) {
  if (l == r) {
    MIN[rt] = pair<int, int>(MIN_init[l], l);
    ps[rt] = ps_init[l];
    return;
  }
  int m = (l + r) >> 1;
  push_down(rt);
  build(l, m, rt << 1);
  build(m + 1, r, rt << 1 | 1);
  push_up(rt);
}
int seg_query(int p, int l, int r, int rt) {
  if (l == r) {
    return ps[rt];
  }
  int m = (l + r) >> 1;
  push_down(rt);
  if (p <= m) return seg_query(p, l, m, rt << 1);
  return seg_query(p, m + 1, r, rt << 1 | 1);
}
int main() {
  scanf("%s", S + 1);
  n = strlen(S + 1);
  long long ans = 0;
  int cnt = 0, mark_num = 0;
  for (int i = 1; i <= n; i++) {
    if (S[i] == '?') {
      cnt--;
      S[i] = ')';
      mark_num++;
      mark_pos[mark_num] = i;
    } else {
      cnt += (S[i] == '(') ? 1 : -1;
    }
    ps_init[i] = cnt;
    MIN_init[i] = INF;
  }
  for (int i = 1; i <= mark_num; i++) {
    int A, B;
    scanf("%d%d", &A, &B);
    ans += B;
    MIN_init[mark_pos[i]] = A - B;
  }
  build(1, n, 1);
  bool ok = true;
  while (ps[1] < 0) {
    int pos = seg_pos(1, n, 1);
    pair<int, int> pi = min_query(1, pos, 1, n, 1);
    if (pi.first == INF) {
      ok = false;
      break;
    }
    ans += pi.first;
    min_update(pi.second, 1, n, 1);
    S[pi.second] = '(';
    seg_add(pi.second, n, 2, 1, n, 1);
  }
  if (ps[1] != 0 || seg_query(n, 1, n, 1)) ok = false;
  if (ok)
    printf("%I64d\n%s\n", ans, S + 1);
  else
    printf("-1\n");
  return 0;
}
