#include <bits/stdc++.h>   using namespace std;   struct B {     int p, id;   };   priority_queue<B> q;   bool operator<(const B &a, const B &b) { return a.p < b.p; }   int main() {     string s;     long long a, b, l, len, cost, i;     B tmp;     cin >> s;     l = 0;     len = s.size();     cost = 0;     for (i = 0; i < len; i++) {       if (s[i] ==  ( )         l++;       else if (s[i] ==  ) )         l--;       else {         cin >> a >> b;         tmp.p = b - a;         tmp.id = i;         q.push(tmp);         s[i] =  ) ;         l--;         cost += b;       }       if (l < 0) {         if (q.empty()) break;         tmp = q.top();         q.pop();         cost -= tmp.p;         s[tmp.id] =  ( ;         l += 2;       }     }     if (l != 0)       cout << -1 << endl;     else {       cout << cost << endl;       cout << s << endl;     }     while (!q.empty()) q.pop();   }   