#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
const int DIM = 50 * 1000 + 100, inf = 1000 * 1000 * 1000;
char s[DIM], s2[DIM];
int ls, cnt, sum, c1[DIM], c2[DIM], tr[4 * DIM], pos[DIM], ch[DIM], pl, k,
    trm[4 * DIM];
long long ans;
vector<pair<int, int> > v1, v2;
void build(int v = 1, int vl = 0, int vr = ls - 1) {
  if (vl == vr) {
    trm[v] = ch[vl];
    return;
  }
  int vs = (vl + vr) / 2;
  build(v * 2, vl, vs);
  build(v * 2 + 1, vs + 1, vr);
  trm[v] = min(trm[v * 2], trm[v * 2 + 1]);
}
void push(int l, int r, int val, int v = 1, int vl = 0, int vr = ls - 1) {
  for (int i = (l); i < (r + 1); i++) ch[i] += val;
}
int get(int l, int r, int dop = 0, int v = 1, int vl = 0, int vr = ls - 1) {
  int res = inf;
  for (int i = (l); i < (r + 1); i++) res = min(res, ch[i]);
  return res;
}
bool can(int nom) {
  while (pl < k && s[pos[pl]] == '(') pl++;
  if (pl == k) return false;
  int mi = get(pos[nom], pos[pl] - 1);
  return (mi >= 2);
}
bool can2(int nom1, int nom2) {
  int mi = get(pos[nom1], pos[nom2] - 1);
  return (mi >= 2);
}
int main() {
  scanf("%s", s);
  memcpy(s2, s, sizeof(s));
  ls = strlen(s);
  for (int i = (0); i < (ls); i++) {
    if (s[i] == '?') {
      pos[cnt++] = i;
    } else if (s[i] == '(')
      sum++;
    else
      sum--;
  }
  for (int i = (0); i < (cnt); i++) {
    scanf("%d%d", &c1[i], &c2[i]);
  }
  int k1 = (cnt - sum) / 2;
  int k2 = (cnt + sum) / 2;
  k = k1 + k2;
  if ((cnt & 1) != (sum & 1) || k1 < 0 || k2 < 0) {
    printf("-1\n");
    return 0;
  }
  int nom = 0;
  for (int i = (0); i < (ls); i++) {
    if (s[i] == '?') {
      if (nom < k1)
        s[i] = '(';
      else
        s[i] = ')';
      nom++;
    }
    if (i > 0)
      ch[i] = ch[i - 1] + ((s[i] == '(') * 2 - 1);
    else
      ch[i] = ((s[i] == '(') * 2 - 1);
  }
  build();
  if (get(0, ls - 1) < 0) {
    printf("-1\n");
    return 0;
  }
  for (int i = (0); i < (k1); i++) {
    v1.push_back(make_pair(c2[i] - c1[i], i));
    ans += c1[i];
  }
  for (int i = (k1); i < (k1 + k2); i++) {
    v2.push_back(make_pair(c1[i] - c2[i], i));
    ans += c2[i];
  }
  pl = k1;
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  int p1 = 0, p2 = 0;
  while (1) {
    while (p1 < k1 && !can(v1[p1].second)) p1++;
    if (p1 == k1) break;
    while (p2 < k2 && !can2(v1[p1].second, v2[p2].second)) p2++;
    if (p2 == k2) break;
    int mb = c2[v1[p1].second] - c1[v1[p1].second] + c1[v2[p2].second] -
             c2[v2[p2].second];
    if (mb >= 0)
      break;
    else
      ans += mb;
    s[pos[v1[p1].second]] = ')';
    s[pos[v2[p2].second]] = '(';
    push(pos[v1[p1].second], pos[v2[p2].second] - 1, -2);
    p1++;
    p2++;
  }
  if (get(ls - 1, ls - 1) < 0)
    while (1)
      ;
  cout << ans << endl;
  cout << s << endl;
  return 0;
}
