#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  vector<int> pri(s.size(), -1);
  int diff = 0;
  long long int total = 0;
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '?') {
      int a, b;
      cin >> a >> b;
      if (a < b) {
        s[i] = '(';
        diff += 1;
        pri[i] = b - a;
        total += a;
      } else {
        s[i] = ')';
        q.push(make_pair(a - b, i));
        total += b;
        diff -= 1;
      }
    } else if (s[i] == '(')
      diff += 1;
    else if (s[i] == ')')
      diff -= 1;
    if (diff < 0) {
      if (q.empty()) {
        cout << -1 << endl;
        return 0;
      }
      s[q.top().second] = '(';
      total += q.top().first;
      q.pop();
      diff += 2;
    }
  }
  if (diff == 0) {
    cout << total << endl;
    cout << s << endl;
    return 0;
  }
  diff = 0;
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q2;
  for (int i = s.size() - 1; i >= 0; i--) {
    if (s[i] == ')')
      diff += 1;
    else if (s[i] == '(')
      diff -= 1;
    if (pri[i] != -1) q2.push(make_pair(pri[i], i));
    if (diff < 0) {
      if (q2.empty()) {
        cout << -1 << endl;
        return 0;
      }
      s[q2.top().second] = ')';
      total += q2.top().first;
      q2.pop();
      diff += 2;
    }
  }
  assert(diff == 0);
  cout << total << endl;
  cout << s << endl;
  return 0;
}
