#include <bits/stdc++.h>
using namespace std;
const int MAXN = 50005;
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    heap;
char s[MAXN];
int main() {
  int x, y;
  scanf("%s", s + 1);
  long long ans = 0;
  int mark = 0, j = 0;
  for (int i = 1; i <= strlen(s + 1); i++) {
    if (s[i] == '?') {
      scanf("%d%d", &x, &y);
      ans += y;
      j++;
      heap.push(make_pair(-y + x, i));
      s[i] = ')';
    }
    if (s[i] == '(')
      mark++;
    else
      mark--;
    if (mark < 0) {
      if (heap.empty()) break;
      ans += heap.top().first;
      s[heap.top().second] = '(';
      heap.pop();
      mark += 2;
    }
  }
  if (mark != 0)
    printf("-1");
  else {
    printf("%I64d\n", ans);
    printf("%s", s + 1);
  }
  return 0;
}
