#include <bits/stdc++.h>
using namespace std;
const int MAX_SIZE = 100000 + 10;
int check(string s) {
  int open = 0, close = 0;
  for (int i = 0; i < s.length(); i++) {
    (s[i] == '(') ? open++ : close++;
    if (open < close) return -1;
  }
  if (open != close)
    return -1;
  else
    return 0;
}
int main() {
  pair<int, int> cost[MAX_SIZE];
  vector<int> Q;
  set<pair<long long, int> > position;
  long long total, num = 0, n;
  string s;
  cin >> s;
  n = s.length();
  for (int i = 0; i < n; i++)
    if (s[i] == '?') num++, Q.push_back(i);
  for (int i = 0; i < num; i++) cin >> cost[i].first >> cost[i].second;
  if (num == 0) {
    if (check(s) == -1)
      cout << -1;
    else
      cout << 0 << endl << s;
    return 0;
  }
  int status = 0, c = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(')
      status++;
    else if (s[i] == ')')
      status--;
    else {
      status--;
      s[i] = ')';
      total += cost[c].second;
      position.insert(pair<long long, int>(cost[c].first - cost[c].second, c));
      c++;
    }
    if (status < 0 && !position.empty()) {
      int u = position.begin()->second;
      position.erase(pair<long long, int>(cost[u].first - cost[u].second, u));
      int pos = Q[u];
      s[pos] = '(';
      total -= cost[u].second;
      total += cost[u].first;
      status++, status++;
    }
  }
  if (status != 0 || check(s) == -1)
    cout << -1;
  else {
    cout << total << endl;
    cout << s;
  }
  return 0;
}
