#include <bits/stdc++.h>
using namespace std;
char str[50005];
int len, a, b;
struct cost {
  int id, v;
  bool operator<(const cost &a) const { return v < a.v; }
};
cost make(int i, int p) {
  cost tmp;
  tmp.id = i;
  tmp.v = p;
  return tmp;
}
priority_queue<cost> m;
int main() {
  scanf("%s", str);
  int cnt = 0, mark = 0;
  long long total = 0;
  len = strlen(str);
  for (int i = 0; i < len; i++) {
    if (str[i] == '(') cnt++;
    if (str[i] == ')' || str[i] == '?') cnt--;
    if (str[i] == '?') {
      str[i] = ')';
      scanf("%d%d", &a, &b);
      m.push(make(i, b - a));
      total += b;
    }
    if (cnt < 0 && m.empty()) {
      mark = 1;
      break;
    }
    if (cnt < 0) {
      cost tmp = m.top();
      m.pop();
      total = total - tmp.v;
      str[tmp.id] = '(';
      cnt += 2;
    }
  }
  if (cnt > 0) mark = 1;
  if (mark)
    printf("-1\n");
  else
    printf("%I64d\n%s\n", total, str);
  return 0;
}
