#include <bits/stdc++.h>
using namespace std;
char str[100000];
struct node {
  int pos;
  int key;
  friend bool operator<(node a, node b) { return a.key > b.key; }
};
void receive(int j) {
  int x, y;
  while (str[j]) {
    if (str[j] == '?') scanf("%d%d", &x, &y);
    j++;
  }
}
long long solve() {
  priority_queue<node> q;
  struct node a;
  int len, j, num;
  long long ans;
  int x, y;
  len = strlen(str);
  if (len & 1) {
    receive(0);
    return -1;
  }
  j = 0;
  ans = 0;
  num = 0;
  while (str[j]) {
    if (str[j] == '(') {
      num++;
    } else if (str[j] == ')') {
      num--;
    } else {
      scanf("%d%d", &x, &y);
      str[j] = ')';
      num--;
      ans += y;
      a.key = x - y;
      a.pos = j;
      q.push(a);
    }
    while (num < 0) {
      if (q.empty()) {
        receive(j);
        return -1;
      }
      a = q.top();
      q.pop();
      ans += a.key;
      str[a.pos] = '(';
      num += 2;
    }
    j++;
  }
  if (num)
    return -1;
  else
    return ans;
}
int main() {
  long long ans;
  while (scanf("%s", str) != EOF) {
    ans = solve();
    if (ans == -1) {
      printf("-1\n");
    } else {
      cout << ans << endl;
      printf("%s\n", str);
    }
  }
  return 0;
}
