#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<long long, int>, vector<pair<long long, int> >,
               greater<pair<long long, int> > >
    q;
char s[50005];
long long x[50005], y[50005];
int main(void) {
  string a, res;
  scanf("%s", &s);
  a = (string)s;
  int kl = 0;
  for (int i = 0; i < a.length(); i++) {
    if (a[i] == '?') kl++;
  }
  for (int i = 0; i < kl; i++) scanf("%lld%lld", &x[i], &y[i]);
  int del = 0;
  long long sum = 0;
  res = a;
  int j = 0;
  bool fail = false;
  for (int i = 0; i < a.length(); i++)
    if (a[i] == '(')
      del++;
    else if (a[i] == ')') {
      del--;
      if (del < 0) {
        if (!q.empty()) {
          pair<long long, int> x = q.top();
          sum += x.first;
          q.pop();
          del += 2;
          res[x.second] = '(';
        } else {
          fail = true;
          break;
        }
      }
    } else {
      sum += y[j];
      q.push(make_pair(x[j] - y[j], i));
      del--;
      res[i] = ')';
      if (del < 0) {
        if (!q.empty()) {
          pair<long long, int> x = q.top();
          q.pop();
          sum += x.first;
          res[x.second] = '(';
          del += 2;
        } else {
          fail = true;
          break;
        }
      }
      j++;
    }
  if (fail || del)
    printf("-1\n");
  else {
    printf("%lld\n", sum);
    printf("%s\n", res.c_str());
  }
  return 0;
}
