#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<int, int> > q;
char str[50005];
int idx[50005];
int qc, len;
int main() {
  qc = 0;
  len = 0;
  int c;
  while (c = getchar()) {
    if (c != '(' && c != ')' && c != '?') break;
    str[len] = (char)c;
    len++;
  }
  int a, b, left = 0;
  long long cost = 0;
  for (int i = 0; i < len; i++) {
    if (str[i] == '(')
      left++;
    else if (str[i] == ')')
      left--;
    else {
      scanf("%d %d", &a, &b);
      str[i] = ')';
      left--;
      cost += b;
      q.push(make_pair(b - a, i));
    }
    if (left < 0) {
      if (q.empty()) break;
      pair<int, int> p = q.top();
      q.pop();
      str[p.second] = '(';
      cost -= p.first;
      left += 2;
    }
  }
  if (left != 0) {
    printf("-1\n");
    return 0;
  }
  cout << cost;
  printf("\n%s\n", str);
  return 0;
}
