#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct node {
  int num, id;
  bool operator<(const node& a) const { return num > a.num; }
};
char s[N];
int main() {
  int x, y;
  while (~scanf("%s", s)) {
    int len = strlen(s);
    int l = 0, r = 0;
    long long ans = 0;
    bool flag = false;
    priority_queue<node, vector<node>, less<node> > Q;
    for (int i = 0; i < len; i++) {
      if (s[i] == '(')
        l++;
      else if (s[i] == ')')
        r++;
      else {
        scanf("%d%d", &x, &y);
        ans += y;
        Q.push((node){x - y, i});
        r++;
      }
      if (l < r) {
        if (Q.empty())
          flag = true;
        else {
          ans += Q.top().num;
          s[Q.top().id] = '(';
          Q.pop();
          l++;
          r--;
        }
      }
    }
    if (l != r || flag)
      printf("-1");
    else {
      printf("%lld\n", ans);
      for (int i = 0; i < len; i++) {
        if (s[i] == '?')
          printf(")");
        else
          printf("%c", s[i]);
      }
    }
    printf("\n");
  }
  return 0;
}
