#include <bits/stdc++.h>
using namespace std;
char s[50010];
long long ans, a, b, c, n;
priority_queue<pair<long long, int> > Q;
int main() {
  scanf("%s", s);
  n = strlen(s);
  for (int i = 0; i < n; i++) {
    if (s[i] == '(')
      c++;
    else {
      c--;
      if (s[i] == '?') {
        scanf("%lld%lld", &a, &b);
        Q.push(make_pair(b - a, i));
        s[i] = ')', ans += b;
      }
    }
    if (c < 0) {
      if (Q.empty()) return puts("-1"), 0;
      if (ans != -1) {
        c += 2;
        ans -= Q.top().first;
        s[Q.top().second] = '(';
        Q.pop();
      }
    }
  }
  if (c != 0) return puts("-1"), 0;
  printf("%lld\n", ans);
  printf("%s", s);
  return 0;
}
