#include <bits/stdc++.h>
using namespace std;
const int siz = 500000;
inline int read() {
  char c = getchar();
  int x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
struct thing {
  int v;
  int at;
  bool operator<(const thing &th) const { return v > th.v; }
} in;
priority_queue<thing> work;
int K;
int L;
int R;
int A;
int a;
char c[siz + 10];
char give[siz + 10];
int s[siz + 10];
int x[siz + 10];
int y[siz + 10];
long long give2;
int main() {
  scanf("%s", c + 1);
  K = strlen(c + 1);
  for (int i = 1; i <= K; i++) {
    if (c[i] == '?') {
      s[i] = 0;
      A++;
    } else if (c[i] == '(')
      s[i] = 1;
    else if (c[i] == ')')
      s[i] = -1;
  }
  for (int i = 1; i <= A; i++) x[i] = read(), y[i] = read();
  for (int i = 1; i <= A; i++) give2 += y[i];
  for (int i = 1; i <= K; i++) {
    if (s[i] == 1) L++;
    if (s[i] == -1) R++;
    if (s[i] == 0) {
      a++;
      R++;
      in.at = i;
      in.v = x[a] - y[a];
      work.push(in);
    }
    if (R > L) {
      if (work.empty()) {
        cout << -1;
        return 0;
      }
      R--;
      L++;
      give2 += work.top().v;
      give[work.top().at] = '(';
      work.pop();
    }
  }
  if (L != R) {
    cout << -1;
    return 0;
  }
  cout << give2 << endl;
  for (int i = 1; i <= K; i++) {
    if (s[i] != 0) {
      if (s[i] > 0)
        cout << '(';
      else
        cout << ')';
      continue;
    }
    if (give[i])
      cout << give[i];
    else
      cout << ')';
  }
  return 0;
}
