#include <bits/stdc++.h>
using namespace std;
char cad[60011];
int ct;
pair<int, int> todos[60011];
int a, b;
char res[60011];
int main() {
  ct = 0;
  scanf("%s", cad);
  while (cin >> a >> b) todos[ct++] = make_pair(a, b);
  bool pode = true;
  int bt = 0;
  set<pair<int, int> > v;
  pair<int, int> cur;
  long long r = 0;
  ct = 0;
  for (int i = 0; i < strlen(cad); i++) {
    if (cad[i] == ')')
      bt--, res[i] = cad[i];
    else if (cad[i] == '(')
      bt++, res[i] = cad[i];
    else if (cad[i] == '?') {
      res[i] = ')';
      r += todos[ct].second;
      v.insert(make_pair(todos[ct].first - todos[ct].second, i));
      ct++;
      bt--;
    }
    if (bt < 0) {
      if (v.size() == 0) pode = false;
      if (!pode) break;
      cur = *(v.begin());
      r += cur.first;
      res[cur.second] = '(';
      v.erase(v.begin());
      bt += 2;
    }
    if (!pode) break;
  }
  if (bt) pode = false;
  if (!pode)
    cout << -1 << endl;
  else {
    cout << r << endl;
    printf("%s\n", res);
  }
}
