#include <bits/stdc++.h>
using namespace std;
const long long N = 500010;
char s[N];
long long a, b;
struct node {
  long long delta;
  char *p;
  bool operator<(const node &W) const { return delta > W.delta; }
};
priority_queue<node> heap;
signed main() {
  scanf("%s", s);
  long long mark = 0, ans = 0;
  for (long long i = 0; s[i] && ans != -1; i++) {
    if (s[i] == '(')
      mark++;
    else {
      mark--;
      if (s[i] == '?') {
        scanf("%d%d", &a, &b);
        s[i] = ')';
        node t;
        t.delta = a - b;
        t.p = s + i;
        heap.push(t);
        ans += b;
      }
    }
    if (mark < 0) {
      if (heap.empty()) ans = -1;
      if (ans != -1) {
        mark += 2;
        node t = heap.top();
        heap.pop();
        *t.p = '(';
        ans += t.delta;
      }
    }
  }
  if (mark) ans = -1;
  cout << ans << endl;
  if (~ans) cout << s << endl;
  return 0;
}
