#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
char a[N];
priority_queue<pair<int, int> > q;
int n;
long long cost;
int main() {
  cin >> a;
  n = strlen(a);
  int nr = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] == '(') ++nr;
    if (a[i] == ')') --nr;
    if (a[i] == '?') {
      a[i] = ')';
      --nr;
      int x, y;
      cin >> x >> y;
      cost += y;
      q.push(pair<int, int>(y - x, i));
    }
    if (nr < 0) {
      if (!q.size()) break;
      a[q.top().second] = '(';
      cost -= q.top().first;
      q.pop();
      nr += 2;
    }
  }
  if (nr) {
    cout << "-1";
    return 0;
  }
  cout << cost << "\n" << a;
  return 0;
}
