#include <bits/stdc++.h>
using namespace std;
auto _ = []() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  return nullptr;
}();
int main() {
  string str;
  long long int cost = 0;
  int open = 0, l, r;
  priority_queue<pair<int, int>> q;
  cin >> str;
  for (long int i = 0; str[i]; i++) {
    open += (str[i] == '(') * 2 - 1;
    if (str[i] == '?') {
      str[i] = ')';
      cin >> l >> r;
      q.push(make_pair(r - l, i));
      cost += r;
    }
    if (open < 0) {
      if (q.empty()) break;
      auto x = q.top();
      q.pop();
      str[x.second] = '(';
      cost -= x.first;
      open += 2;
    }
  }
  if (open)
    cout << -1 << endl;
  else
    cout << cost << endl << str << endl;
  return 0;
}
