#include <bits/stdc++.h>
using namespace std;
const int mn = 5 * 10005;
char c[mn], ans[mn];
int n;
long long x[mn], y[mn];
map<int, int> mp;
long long l = 0, r = 0, res = 0, t = 0;
priority_queue<pair<long long, int> > q;
int main() {
  scanf("%s", c);
  n = strlen(c);
  for (int i = 0; i < n; i++) {
    ans[i] = c[i];
    if (c[i] == '?') {
      scanf("%I64d%I64d", &x[t], &y[t]);
      mp[i] = t;
      ans[i] = ')';
      res += y[t];
      t++;
    }
  }
  if (c[n - 1] == '(' || c[0] == ')') {
    cout << -1;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (c[i] == '(')
      l++;
    else if (c[i] == ')') {
      r++;
      if (l < r) {
        if (!q.size()) {
          cout << -1;
          return 0;
        }
        int k = -q.top().first, t = q.top().second;
        q.pop();
        ans[t] = '(';
        r--, l++;
        res += k;
      }
    } else {
      r++;
      long long f = x[mp[i]] - y[mp[i]];
      q.push(make_pair(-f, i));
      if (l < r) {
        int k = -q.top().first, t = q.top().second;
        q.pop();
        ans[t] = '(';
        r--, l++;
        res += k;
      }
    }
  }
  if (l != r) {
    cout << -1;
    return 0;
  }
  cout << res << endl;
  printf("%s", ans);
  return 0;
}
