#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
string st;
int n, open[N], close[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> st;
  int n = (int)st.size();
  for (int i = (int)0; i <= (int)n - 1; ++i)
    if (st[i] == '?') {
      cin >> open[i] >> close[i];
    }
  set<pair<int, int> > s;
  int bal = 0;
  long long res = 0;
  for (int i = (int)0; i <= (int)(int)st.size() - 1; ++i) {
    if (st[i] == '(')
      ++bal;
    else if (st[i] == ')')
      --bal;
    else {
      st[i] = ')';
      res += close[i];
      --bal;
      s.insert(make_pair(open[i] - close[i], i));
    }
    if (bal < 0) {
      bal += 2;
      if (s.empty()) return cout << -1, 0;
      auto first = s.begin();
      res += first->first;
      s.erase(first);
      st[first->second] = '(';
    }
  }
  if (bal) return cout << -1, 0;
  cout << res << "\n" << st << endl;
  return 0;
}
