#include <bits/stdc++.h>
using namespace std;
struct d {
  int delta;
  char *p;
  bool operator<(const d &b) const { return delta > b.delta; }
} t;
priority_queue<d> q;
char s[50002];
int main() {
  int mark = 0;
  long long ans = 0;
  scanf("%s", s);
  for (int i = 0, a, b; s[i] && ans != -1; i++) {
    if (s[i] == '(')
      ++mark;
    else {
      --mark;
      if (s[i] == '?') {
        scanf("%d%d", &a, &b);
        t.delta = a - b;
        t.p = s + i;
        q.push(t);
        s[i] = ')';
        ans += b;
      }
    }
    if (mark < 0) {
      if (q.empty()) ans = -1;
      if (ans != -1) {
        mark += 2;
        t = q.top();
        q.pop();
        *t.p = '(';
        ans += t.delta;
      }
    }
  }
  if (mark) ans = -1;
  printf("%I64d", ans);
  if (ans != -1) printf("\n%s", s);
  return 0;
}
