#include <bits/stdc++.h>
using namespace std;
char buf[1 << 20];
const int maxn = (1 << 20);
int a[maxn][2];
char how[maxn];
int main() {
  gets(buf);
  string s(buf);
  int cnt = 0;
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '?') cnt++;
  for (int i = 0; i < cnt; i++) assert(scanf("%d%d", &a[i][0], &a[i][1]) == 2);
  set<pair<int, int> > all;
  int level = 0;
  long long score = 0;
  int cur = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '(')
      level++;
    else if (s[i] == ')') {
      if (level) {
        level--;
      } else if (all.empty()) {
        puts("-1");
        return 0;
      } else {
        int where = all.begin()->second;
        int penalty = all.begin()->first;
        all.erase(all.begin());
        how[where] ^= 1;
        score += penalty;
        level++;
      }
    } else if (s[i] == '?') {
      if (a[cur][0] <= a[cur][0]) {
        score += a[cur][0];
        how[i] = 0;
        level++;
      } else {
        how[i] = 1;
        score += a[cur][1];
        all.insert(pair<int, int>(a[cur][0] - a[cur][1], i));
        level--;
        if (level < 0) {
          int where = all.begin()->second;
          int penalty = all.begin()->first;
          all.erase(all.begin());
          how[where] ^= 1;
          score += penalty;
          level += 2;
          assert(level >= 0);
        }
      }
      cur++;
    }
  }
  reverse(s.begin(), s.end());
  reverse(a, a + cnt);
  reverse(how, how + s.size());
  all.clear();
  level = 0;
  cur = 0;
  for (int i = 0; i < cnt; i++) swap(a[i][0], a[i][1]);
  for (int i = 0; i < s.size(); i++) how[i] ^= 1;
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '(')
      s[i] = ')';
    else if (s[i] == ')')
      s[i] = '(';
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '(')
      level++;
    else if (s[i] == ')') {
      if (level)
        level--;
      else if (all.empty()) {
        puts("-1");
        return 0;
      } else {
        int where = all.begin()->second;
        int penalty = all.begin()->first;
        all.erase(all.begin());
        how[where] ^= 1;
        score += penalty;
        level++;
      }
    } else if (s[i] == '?') {
      if (how[i] == 0) {
        level++;
      } else {
        level--;
        all.insert(pair<int, int>(a[cur][0] - a[cur][1], i));
        if (level < 0) {
          level += 2;
          int where = all.begin()->second;
          int penalty = all.begin()->first;
          all.erase(all.begin());
          how[where] ^= 1;
          score += penalty;
        }
      }
      cur++;
    }
  }
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '?')
      if (how[i])
        s[i] = ')';
      else
        s[i] = '(';
  reverse(s.begin(), s.end());
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '(')
      s[i] = ')';
    else if (s[i] == ')')
      s[i] = '(';
  if (level != 0) {
    puts("-1");
    return 0;
  }
  cout << score << endl;
  cout << s << endl;
  return 0;
}
