#include <bits/stdc++.h>
using namespace std;
struct Tree {
  int left, right;
  long long value, tag;
  Tree *left_child, *right_child;
  Tree() {}
} * root;
const int N = 5e4 + 5;
Tree t[N * 8];
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    pq, tq;
char s[N];
int d[N], b[N];
int n, m, Size = 1, tot;
long long ans = 0;
void increase(Tree *pos, long long val) {
  if (pos->left == pos->right)
    pos->value += val;
  else
    pos->tag += val;
}
void update(Tree *pos) {
  pos->value = min(pos->left_child->value + pos->left_child->tag,
                   pos->right_child->value + pos->right_child->tag);
}
void downdate(Tree *pos) {
  increase(pos->left_child, pos->tag);
  increase(pos->right_child, pos->tag);
  pos->tag = 0LL;
}
void build(Tree *pos, int x, int y) {
  pos->left = x;
  pos->right = y;
  pos->value = 0LL;
  pos->tag = 0LL;
  if (x == y) return;
  int mid = (x + y) >> 1;
  pos->left_child = t + Size;
  pos->right_child = t + Size + 1;
  int siz = Size;
  Size += 2;
  build(t + siz, x, mid);
  build(t + siz + 1, mid + 1, y);
  update(pos);
}
void add(Tree *pos, int x, int y, long long val) {
  if (pos->left >= x && pos->right <= y) {
    increase(pos, val);
    return;
  }
  if (pos->right < x || pos->left > y) return;
  add(pos->left_child, x, y, val);
  add(pos->right_child, x, y, val);
  update(pos);
}
long long query(Tree *pos, int x, int y) {
  if (pos->left >= x && pos->right <= y) return pos->value + pos->tag;
  if (pos->right < x || pos->left > y) return 9223372036854775807LL;
  downdate(pos);
  update(pos);
  return min(query(pos->left_child, x, y), query(pos->right_child, x, y));
}
void debug(Tree *pos) {
  if (pos->left == pos->right) {
    printf("left:%d right:%d val:%I64d\n", pos->left, pos->right, pos->value);
    return;
  }
  printf(
      "left:%d right:%d val:%I64d tag:%I64d left_child(%d,%d) "
      "right_child(%d,%d)\n",
      pos->left, pos->right, pos->value, pos->tag, pos->left_child->left,
      pos->left_child->right, pos->right_child->left, pos->right_child->right);
  debug(pos->left_child);
  debug(pos->right_child);
}
void ddb(Tree *pos, long long vl) {
  if (pos->left == pos->right) {
    printf("left:%d right:%d val:%I64d\n", pos->left, pos->right,
           pos->value + vl);
    return;
  }
  ddb(pos->left_child, vl + pos->tag);
  ddb(pos->right_child, vl + pos->tag);
}
int main() {
  root = t;
  scanf("%s", s);
  n = strlen(s);
  build(root, 1, n);
  for (int i = 0; i < n; i++) {
    if (s[i] == '?')
      b[m++] = i + 1, tot++, s[i] = '(';
    else
      tot += ((s[i] == '(') ? 1 : -1);
    add(root, i + 1, i + 1, tot);
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    ans += x;
    pq.push(make_pair(y - x, b[i]));
  }
  if (query(root, 1, n) < 0) {
    puts("-1");
    return 0;
  }
  while (tot) {
    bool flag = false;
    while (!pq.empty()) {
      int vl = pq.top().first;
      int id = pq.top().second;
      pq.pop();
      if (min(query(root, 1, id - 1), query(root, id, n) - 2) >= 0) {
        tot -= 2;
        ans += vl;
        flag = true;
        s[id - 1] = ')';
        add(root, id, n, -2);
        break;
      }
    }
    if (!flag && pq.empty()) {
      puts("-1");
      return 0;
    }
  }
  printf("%I64d\n", ans);
  puts(s);
  return 0;
}
