#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
inline void remax(T1& A, T2 B) {
  if (A < B) A = B;
}
template <class T1, class T2>
inline void remin(T1& A, T2 B) {
  if (A > B) A = B;
}
string ToString(long long num) {
  string ret;
  bool neg = false;
  if (num < 0) num *= ~0, neg = true;
  do {
    ret += ((num % 10) + '0');
    num /= 10;
  } while (num);
  if (neg) ret += '-';
  reverse(ret.begin(), ret.end());
  return ret;
}
long long ToNumber(string s) {
  long long r = 0, p = 1;
  int e = (s[0] == '-');
  for (int i = (int)s.size() - 1; i >= e; --i) r += (s[i] - '0') * p, p *= 10;
  if (e) r *= ~0;
  return r;
}
long long Gcd(long long a, long long b) {
  if (!a || !b) return a + b;
  while (a %= b ^= a ^= b ^= a)
    ;
  return b;
}
long long Power(long long base, unsigned long long power) {
  long long ret = 1;
  while (power) {
    if (power & 1) ret *= base;
    power >>= 1;
    base *= base;
  }
  return ret;
}
long long PowerMod(long long base, unsigned long long power, long long mod) {
  if (!power) return 1 % mod;
  if (power & 1) return (base * PowerMod(base, power - 1, mod)) % mod;
  return PowerMod((base * base) % mod, power >> 1, mod);
}
int Log(long long num, long long base) {
  int ret = 0;
  while (num) {
    ++ret;
    num /= base;
  }
  return ret;
}
inline void run() {
  char s[50001];
  priority_queue<pair<long long, int>> q;
  long long ans = 0;
  int cnt = 0;
  scanf("%s", s);
  for (int i = 0; s[i]; ++i) {
    if (s[i] == '(')
      ++cnt;
    else if (s[i] == ')')
      --cnt;
    else {
      int a, b;
      scanf("%d%d", &a, &b);
      s[i] = ')';
      q.emplace(b - a, -i);
      ans += b;
      --cnt;
    }
    if (cnt == ~0) {
      if (q.empty()) {
        while (s[++i])
          if (s[i] == '?') scanf("%*d%*d");
        return (void)puts("-1");
      }
      auto p = q.top();
      q.pop();
      s[-p.second] = '(';
      ans += -p.first;
      ++ ++cnt;
    }
  }
  if (cnt)
    puts("-1");
  else
    printf("%I64d\n%s\n", ans, s);
}
int main() {
  run();
  return 0;
}
