#include <bits/stdc++.h>
using namespace std;
int cond = 1;
const int M = 50000;
int cost[M];
class cmp {
 public:
  bool operator()(int a, int b) { return cost[a] < cost[b]; }
};
int main() {
  int len = 0;
  size_t explen = 0;
  string s;
  getline(cin, s);
  len = s.length();
  char* buff = new char[len + 1];
  buff[len] = NULL;
  memcpy(buff, s.c_str(), len);
  vector<int> indices;
  while ((len > 0) && (buff[len - 1] == '\n')) {
    buff[--len] = NULL;
  }
  for (int i = 0; i < len; ++i) {
    cost[i] = 0;
    if (buff[i] == '?') {
      indices.push_back(i);
    }
  }
  long long sum = 0;
  for (int i = 0; i < indices.size(); ++i) {
    int a, b;
    scanf(" %d %d", &a, &b);
    sum += a;
    cost[indices[i]] = b - a;
  }
  multiset<int, cmp> choices;
  int low = 0;
  for (int i = 0; i < len; ++i) {
    if (buff[i] == '(') {
      ++low;
    } else if (buff[i] == ')') {
      --low;
    } else {
      --low;
      buff[i] = '(';
      choices.insert(i);
    }
    if (low < 0) {
      if (choices.empty()) {
        printf("-1\n");
        return 0;
      }
      choices.erase((++choices.rbegin()).base());
      low += 2;
    }
  }
  if (low != 0) {
    printf("-1\n");
    return 0;
  }
  for (set<int>::iterator it = choices.begin(); it != choices.end(); ++it) {
    sum += cost[*it];
    buff[*it] = ')';
  }
  printf("%I64d\n", sum);
  printf("%s\n", buff);
  return 0;
}
