#include <bits/stdc++.h>
using namespace std;
const int maxn = 50010;
int a[maxn], b[maxn], pos[maxn];
int max_, sum_;
long long res;
char s[maxn];
struct Tree {
  int l, r, suml, sumr, maxl, maxr;
  Tree *ch[2];
  void Update() {
    suml = ch[0]->suml + ch[1]->suml;
    sumr = ch[0]->sumr + ch[1]->sumr;
    maxl = max(ch[0]->maxl, ch[0]->suml + ch[1]->maxl);
    maxr = max(ch[1]->maxr, ch[1]->sumr + ch[0]->maxr);
  }
  Tree(int l_, int r_) : l(l_), r(r_) {
    if (l + 1 == r) {
      if (s[l] == '?')
        suml = sumr = maxl = maxr = -1;
      else if (s[l] == '(') {
        suml = maxl = -1;
        sumr = maxr = 1;
      } else {
        suml = maxl = 1;
        sumr = maxr = -1;
      }
      return;
    }
    ch[0] = new Tree(l, (l + r) >> 1);
    ch[1] = new Tree((l + r) >> 1, r);
    Update();
  }
  void Quel(int ll, int rr) {
    if (ll <= l && rr >= r) {
      max_ = max(max_, sum_ + maxl);
      return;
    }
    if (ll < (l + r) >> 1) ch[0]->Quel(ll, rr);
    if (rr > (l + r) >> 1) {
      sum_ += ch[0]->suml;
      ch[1]->Quel(ll, rr);
      sum_ -= ch[0]->suml;
    }
    Update();
  }
  void Quer(int ll, int rr) {
    if (ll <= l && rr >= r) {
      max_ = max(max_, sum_ + maxr);
      return;
    }
    if (rr > (l + r) >> 1) ch[1]->Quer(ll, rr);
    if (ll < (l + r) >> 1) {
      sum_ += ch[1]->sumr;
      ch[0]->Quer(ll, rr);
      sum_ -= ch[1]->sumr;
    }
    Update();
  }
  void Modify(int whi, int x) {
    if (l + 1 == r) {
      if (x == 0) {
        suml = maxl = -1;
        sumr = maxr = 1;
      } else {
        suml = maxl = 1;
        sumr = maxr = -1;
      }
      return;
    }
    ch[whi >= (l + r) >> 1]->Modify(whi, x);
    Update();
  }
} * root;
struct Rec {
  int id, key;
  bool operator<(const Rec &o) const { return key > o.key; }
} c[maxn];
void settle(int whi, int x) {
  if (x == 0) {
    res += a[whi];
    s[whi] = '(';
    root->Modify(whi, x);
  } else {
    res += b[whi];
    s[whi] = ')';
    root->Modify(whi, x);
  }
}
int main() {
  scanf("%s", s);
  int n = strlen(s);
  int m = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '?') pos[m++] = i;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a[pos[i]], &b[pos[i]]);
    c[i].id = i;
    c[i].key = abs(a[pos[i]] - b[pos[i]]);
  }
  sort(c, c + m);
  root = new Tree(0, n);
  if (root->maxl > 0 || root->maxr > 0) {
    puts("-1");
    return 0;
  }
  res = 0;
  for (int i = 0; i < m; i++) {
    int whi = pos[c[i].id];
    if (a[whi] >= b[whi]) {
      sum_ = 0;
      max_ = -10;
      root->Quel(whi, n);
      if (max_ <= -2)
        settle(whi, 1);
      else
        settle(whi, 0);
    } else {
      sum_ = 0;
      max_ = -10;
      root->Quer(0, whi + 1);
      if (max_ <= -2)
        settle(whi, 0);
      else
        settle(whi, 1);
    }
  }
  cout << res << endl;
  printf("%s\n", s);
  return 0;
}
