#include <bits/stdc++.h>
using namespace std;

const double PI = acos(-1);
using ll = long long;
using ull = unsigned long long;
const int inf = 2e9;
const ll INF = 4e18;
const ll MOD = 1e9+7;
typedef pair<ll,int> P;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second

ll a[50010], b[50010];

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s;
  cin >> s;
  ll sum = 0;
  priority_queue<P,vector<P>,greater<P>> pq;
  REP(i,sz(s)) {
    if (s[i] == '(') {
      a[i] = 0;
      b[i] = inf;
    } else if (s[i] == ')') {
      a[i] = 0;
      b[i] = inf;
    }
    if (s[i] == '?') {
      s[i] = '(';
      cin >> a[i] >> b[i];
      sum += a[i];
    }
  }
  int k = 0;
  for (int i = sz(s) - 1; i >= 0; i--) {
    if (s[i] == ')') k++;
    else {
      k--;
      pq.push({b[i] - a[i], i});
      if (k < 0) {
        P tp = pq.top(); pq.pop();
        sum += tp.fi;
        s[tp.se] = ')';
        k+=2;
      }
    }
  }
  if (sum >= inf) cout << -1 << endl;
  else {
  cout << sum << endl;
  cout << s << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

const double PI = acos(-1);
using ll = long long;
using ull = unsigned long long;
const int inf = 2e9;
const ll INF = 4e18;
const ll MOD = 1e9+7;
typedef pair<double,pair<int,int>> P;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define pb push_back
#define fi first
#define se second

ll dp[5010][2510];
string s;
int n;
ll a[5010];
ll b[5010];
vector<char> ans;

void re(int x, int y) {
  if (x == 0) return;
  if (s[x-1] == ')') {
    re(x-1,y+1);
    ans.pb(')');
  } else if (s[x-1] == '(') {
    re(x-1,y-1);
    ans.pb('(');
  } else {
    if (dp[x][y] == dp[x-1][y-1] + a[x-1]) {
      re(x-1,y-1);
      ans.pb('(');
    } else {
      re(x-1,y+1);
      ans.pb(')');
    }
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> s;
  n = s.size();
  REP(i,n+2) REP(j,2510) dp[i][j] = INF;
  REP(i,n) if (s[i] == '?') cin >> a[i] >> b[i];
  dp[0][0] = 0;
  REP(i,n) REP(j,n) {
    if (s[i] == ')') {
      if (j <= n/2) dp[i+1][j] = min(dp[i+1][j], dp[i][j+1]);
    } else if (s[i] == '(') {
      if (j != 0 && j <= n/2) dp[i+1][j] = min(dp[i+1][j], dp[i][j-1]);
    } else {
      if (j != 0 && j <= n/2)
        dp[i+1][j] = min(dp[i+1][j], dp[i][j-1] + a[i]);
      if (j <= n/2)
        dp[i+1][j] = min(dp[i+1][j], dp[i][j+1] + b[i]);
    }
  }
  cout << dp[n][0] << endl;
  re(n, 0);
  REP(i,n) cout << ans[i];
  cout << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

const double PI = acos(-1);
using ll = long long;
using ull = unsigned long long;
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9+7;
typedef pair<ll,int> P;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second

ll a[50010], b[50010];

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s;
  cin >> s;
  ll sum = 0;
  priority_queue<P,vector<P>,greater<P>> pq;
  REP(i,sz(s)) {
    if (s[i] == '(') {
      a[i] = 0;
      b[i] = INF;
    } else if (s[i] == ')') {
      a[i] = 0;
      b[i] = INF;
    }
    if (s[i] == '?') {
      s[i] = '(';
      cin >> a[i] >> b[i];
      sum += a[i];
    }
  }
  int k = 0;
  for (int i = sz(s) - 1; i >= 0; i--) {
    if (s[i] == ')') k++;
    else {
      k--;
      pq.push({b[i] - a[i], i});
      if (k < 0) {
        P tp = pq.top(); pq.pop();
        sum += tp.fi;
        s[tp.se] = ')';
        k+=2;
      }
    }
  }
  if (sum >= INF) cout << -1 << endl;
  else {
  cout << sum << endl;
  cout << s << endl;
  }
  return 0;
}


// lose small or win big.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using ll = int64_t;
using ld = long double;
using ull = uint64_t;
using namespace std;
using namespace __gnu_pbds;
typedef vector <int> vi;
typedef pair <int, int> ii;

const int INF = 1 << 30;

#define maxn 100010

int main() {
#ifdef BZ
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.setf(ios::fixed); cout.precision(6);

    string a;
    cin >> a;

    set <ii> s;

    int v = 0;
    ll res = 0;
    int x, y;

    for(int i = 0; i < a.length(); i++) {
        if(a[i] == '(') v++;
        else if(a[i] == ')') v--;
        else {
            a[i] = ')';
            v--;
            cin >> x >> y;
            res += y;
            s.insert({x - y, i});
        }
        if(v < 0) {
            if(s.empty()) {
                cout << "-1";
                return 0;
            }
            auto t = *s.begin();
            s.erase(s.begin());
            a[t.second] = '(';
            v += 2;
            res += t.first;
        }
    }

    if(v) {
        cout << -1 << "\n";
        return 0;
    }

    cout << res << "\n";
    cout << a << "\n";

}


// lose small or win big.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using ll = int64_t;
using ld = long double;
using ull = uint64_t;
using namespace std;
using namespace __gnu_pbds;
typedef vector <int> vi;
typedef pair <int, int> ii;

const int INF = 1 << 30;

#define maxn 100010

int main() {
#ifdef BZ
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.setf(ios::fixed); cout.precision(6);

    string a;
    cin >> a;

    set <ii> s;

    int v = 0;
    ll res = 0;
    int x, y;

    for(int i = 0; i < a.length(); i++) {
        if(a[i] == '(') v++;
        else if(a[i] == ')') v--;
        else {
            a[i] = ')';
            v--;
            cin >> x >> y;
            res += y;
            s.insert({x - y, i});
        }
        if(v < 0) {
            if(s.empty()) {
                cout << "-1";
                return 0;
            }
            auto t = *s.begin();
            s.erase(s.begin());
            a[t.second] = '(';
            v += 2;
            res += t.first;
        }
    }

    if(v < 0) {
        cout << -1 << "\n";
        return 0;
    }

    cout << res << "\n";
    cout << a << "\n";

}


// lose small or win big.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using ll = int64_t;
using ld = long double;
using ull = uint64_t;
using namespace std;
using namespace __gnu_pbds;
typedef vector <int> vi;
typedef pair <int, int> ii;

const int INF = 1 << 30;

#define maxn 100010

int main() {
#ifdef BZ
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.setf(ios::fixed); cout.precision(6);

    string a;
    cin >> a;

    set <ii> s;

    int v = 0;
    ll res = 0;
    int x, y;

    for(int i = 0; i < a.length(); i++) {
        if(a[i] == '(') v++;
        else if(a[i] == ')') v--;
        else {
            a[i] = ')';
            v--;
            cin >> x >> y;
            res += y;
            s.insert({x - y, i});
        }
        if(v < 0) {
            if(s.empty()) {
                cout << "-1";
                return 0;
            }
            auto t = *s.begin();
            s.erase(s.begin());
            a[t.second] = '(';
            v += 2;
            res += t.first;
        }
    }

    cout << res << "\n";
    cout << a << "\n";

}


// BE NAME KHODA :)))
#include <bits/stdc++.h>
using namespace std;
const int MX=1e5;

int n,a,b,c,sum;
set<pair<int,int>>s;
string s1;

int main()
{
    cin>>s1;
    for(int i=0;i<s1.size();i++){
        if(s1[i]=='(')c++;
        if(s1[i]==')')c--;
        if(s1[i]=='?'){
            cin>>a>>b;
            s1[i]=')';
            c--;
            sum+=b;
            s.insert({a-b,i});
        }
        while(c<0 && s.size()){
            int q1=(*s.begin()).first;
            int q2=(*s.begin()).second;
            //cout<<q1<<" "<<q2<<endl;
            s.erase(s.begin());
            c+=2;
            sum+=q1;
            s1[q2]='(';
        }
        //cout<<c<<endl;
        if(c<0)break;
    }
    if(c!=0)cout<<-1;
    else cout<<sum<<endl<<s1;

}


//BE NAME KHODA:))))
#include <bits/stdc++.h>
//BORDE KOSHTI RO TABRIK MIGAM!
using namespace std;
typedef long long ll;
const int MX=1e5+100;

vector<vector<pair<int,int>>>v2;
vector<pair<int,int>>v;

int n,a[MX],flag;
bool shet[MX];

int main()
{
    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];

    while(1){
        flag=0;
        fill(shet+1,shet+n+1,0);
        v.clear();
        for(int i=1;i<=n;i++){
            if(a[i]!=i && !shet[i] && !shet[a[i]]){
                flag=1;
                shet[i]=1;
                shet[a[i]]=1;
                v.push_back({i,a[i]});
                int y=a[i];
                a[i]=a[a[i]];
                a[y]=y;
            }
        }
        //for(int i=1;i<=n;i++)cout<<a[i]<<" ";
        //cout<<endl;
        //cout<<flag<<endl;
        if(!flag)break;
        v2.push_back(v);
    }
    //cout<<endl;
    cout<<v2.size()<<endl;
    for(auto i:v2){
        cout<<i.size()<<endl;
        for(auto j:i){
            cout<<j.first<<" "<<j.second<<endl;
        }
    }
}


// BE NAME KHODA :)))
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=1e5;

ll n,a,b,c,sum;
set<pair<ll,ll>>s;
string s1;

int main()
{
    cin>>s1;
    for(int i=0;i<s1.size();i++){
        if(s1[i]=='(')c++;
        if(s1[i]==')')c--;
        if(s1[i]=='?'){
            cin>>a>>b;
            s1[i]=')';
            c--;
            sum+=b;
            s.insert({a-b,i});
        }
        while(c<0 && s.size()){
            int q1=(*s.begin()).first;
            int q2=(*s.begin()).second;
            //cout<<q1<<" "<<q2<<endl;
            s.erase(s.begin());
            c+=2;
            sum+=q1;
            s1[q2]='(';
        }
        //cout<<c<<endl;
        if(c<0)break;
    }
    if(c!=0)cout<<-1;
    else cout<<sum<<endl<<s1;

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
struct node{
    int num,lab;
    bool operator<(const node& a)const{
        return num>a.num;
    }
}e[50005];
char s[50005];
priority_queue<node,vector<node>,less<node> >sp;
int main(){
    int n,i,j;
    scanf("%s",s+1);
    int len=strlen(s+1);
    ll ans=0;
    int l=0,r=0,flag=1,x,y;
    for(i=1;i<=len;i++){
        if(s[i]=='(')l++;
        else if(s[i]==')')r++;
        else{
            scanf("%d%d",&x,&y);
            ans+=y;
            sp.push((node){x-y,i});
            r++;
        }
        if(l<r){
            if(sp.empty())flag=0;
            else{
                ans+=sp.top().num;
                s[sp.top().lab]='(';
                sp.pop();
                r--;
                l++;
            }
        }
    }
    if(!flag)printf("-1\n");
    else{
        printf("%lld\n",ans);
        for(i=1;i<=len;i++)printf("%c",s[i]=='?'?')':s[i]);
        printf("\n");
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
struct node{
    int num,lab;
    bool operator<(const node& a)const{
        return num>a.num;
    }
}e[50005];
char s[50005];
priority_queue<node,vector<node>,less<node> >sp;
int main(){
    int n,i,j;
    scanf("%s",s+1);
    int len=strlen(s+1);
    ll ans=0;
    int l=0,r=0,flag=1,x,y;
    for(i=1;i<=len;i++){
        if(s[i]=='(')l++;
        else if(s[i]==')')r++;
        else{
            scanf("%d%d",&x,&y);
            ans+=y;
            sp.push((node){x-y,i});
            r++;
        }
        if(l<r){
            if(sp.empty())flag=0;
            else{
                ans+=sp.top().num;
                s[sp.top().lab]='(';
                sp.pop();
                r--;
                l++;
            }
        }
    }
    if(!flag||s[len]=='(')printf("-1\n");
    else{
        printf("%lld\n",ans);
        for(i=1;i<=len;i++)printf("%c",s[i]=='?'?')':s[i]);
        printf("\n");
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
struct node{
    int num,lab;
    bool operator<(const node& a)const{
        return num>a.num;
    }
}e[50005];
char s[50005];
priority_queue<node,vector<node>,less<node> >sp;
int main(){
    int n,i,j;
    scanf("%s",s+1);
    int len=strlen(s+1);
    ll ans=0;
    int l=0,r=0,flag=1,x,y;
    for(i=1;i<=len;i++){
        if(s[i]=='(')l++;
        else if(s[i]==')')r++;
        else{
            scanf("%d%d",&x,&y);
            ans+=y;
            sp.push((node){x-y,i});
            r++;
        }
        if(l<r){
            if(sp.empty())flag=0;
            else{
                ans+=sp.top().num;
                s[sp.top().lab]='(';
                sp.pop();
                r--;
                l++;
            }
        }
    }
    if(!flag||l!=r)printf("-1\n");
    else{
        printf("%lld\n",ans);
        for(i=1;i<=len;i++)printf("%c",s[i]=='?'?')':s[i]);
        printf("\n");
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
const int N = 5e4 + 5;
char str[N];
set<pii> st;

int main() {
    scanf("%s", str);
    int a, b;
    int x = 0;
    long long ans = 0;
    for (int i=0; str[i]; ++i) {
        if (str[i] == '(') x--;
        else if (str[i] == ')') x++;
        else {
            scanf("%d%d", &a, &b);
            str[i] = ')';
            x++;
            ans += b;
            st.insert(pii(a-b, i));
        }
        while (x > 0) {
            if (st.empty()) {
                puts("-1");
                return 0;
            }
            pii p = *st.begin();
            ans += p.first;
            str[p.second] = '(';
            st.erase(p);
            x -= 2;
        }
    }
    printf("%I64d\n", ans);
    puts(str);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
const int N = 5e4 + 5;
char str[N];
set<pii> st;

int main() {
    scanf("%s", str);
    int a, b;
    int x = 0;
    long long ans = 0;
    for (int i=0; str[i]; ++i) {
        if (str[i] == '(') x--;
        else if (str[i] == ')') x++;
        else {
            scanf("%d%d", &a, &b);
            str[i] = ')';
            x++;
            ans += b;
            st.insert(pii(a-b, i));
        }
        while (x > 0) {
            if (st.empty()) {
                puts("0");
                return 0;
            }
            pii p = *st.begin();
            ans += p.first;
            str[p.second] = '(';
            st.erase(p);
            x -= 2;
        }
    }
    printf("%I64d\n", ans);
    puts(str);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
const int N = 5e4 + 5;
char str[N];
set<pii> st;

int main() {
    scanf("%s", str);
    int a, b;
    int x = 0;
    long long ans = 0;
    for (int i=0; str[i]; ++i) {
        if (str[i] == '(') x--;
        else if (str[i] == ')') x++;
        else {
            scanf("%d%d", &a, &b);
            str[i] = ')';
            x++;
            ans += b;
            st.insert(pii(a-b, i));
        }
        while (x > 0) {
            if (st.empty()) {
                puts("-1");
                return 0;
            }
            pii p = *st.begin();
            ans += p.first;
            str[p.second] = '(';
            st.erase(p);
            x -= 2;
        }
    }
    if (x != 0) {
        puts("-1");
        return 0;
    }
    printf("%I64d\n", ans);
    puts(str);
    return 0;
}


#include "stdafx.h"
#include <iostream>
#include <queue>
#include <string>
using namespace std;
struct node {
	int id;
	int v;
	bool operator < (const node& a) const {
		return v < a.v;
	}
};
node make(int p, int v) {
	node ret;
	ret.id = p;
	ret.v = v;
	return ret;
}
int main() {
	string str;
	int a, b, len;
	long long int ans, cnt;
	cin.sync_with_stdio(false);
	priority_queue<node> q;
	while (cin >> str) {
		while (q.size()) {
			q.pop();
		}
		ans = 0;
		cnt = 0;
		len = str.length();
		for (int i = 0; i < len; i++) {
			cnt += str[i] == '(';
			cnt -= (str[i] == ')' || str[i] == '?');
			if (str[i] == '?') {
				cin >> a >> b;
				q.push(make(i, b - a));
				ans += b;
				str[i] = ')';
			}
			if (cnt < 0 && (!q.size())) {
				cout << -1 << endl;
				return 0;
			}
			if (cnt < 0) {
				node now = q.top();
				q.pop();
				ans -= now.v;
				str[now.id] = '(';
				cnt += 2;
			}
		}
		if (cnt < 0) {
			cout << -1 << endl;
		}
		else {
			cout << ans << endl;
			cout << str << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <queue>
#include <string>
using namespace std;
struct node {
	int id;
	int v;
	bool operator < (const node& a) const {
		return v < a.v;
	}
};
node make(int p, int v) {
	node ret;
	ret.id = p;
	ret.v = v;
	return ret;
}
int main() {
	string str;
	int a, b, len;
	long long int ans, cnt;
	cin.sync_with_stdio(false);
	priority_queue<node> q;
	while (cin >> str) {
		while (q.size()) {
			q.pop();
		}
		ans = 0;
		cnt = 0;
		len = str.length();
		for (int i = 0; i < len; i++) {
			cnt += str[i] == '(';
			cnt -= (str[i] == ')' || str[i] == '?');
			if (str[i] == '?') {
				cin >> a >> b;
				q.push(make(i, b - a));
				ans += b;
				str[i] = ')';
			}
			if (cnt < 0 && (!q.size())) {
				cout << -1 << endl;
				return 0;
			}
			if (cnt < 0) {
				node now = q.top();
				q.pop();
				ans -= now.v;
				str[now.id] = '(';
				cnt += 2;
			}
		}
		if (cnt != 0) {
			cout << -1 << endl;
		}
		else {
			cout << ans << endl;
			cout << str << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <queue>
#include <string>
using namespace std;
struct node {
	int id;
	int v;
	bool operator < (const node& a) const {
		return v < a.v;
	}
};
node make(int p, int v) {
	node ret;
	ret.id = p;
	ret.v = v;
	return ret;
}
int main() {
	string str;
	int a, b, len;
	long long int ans, cnt;
	cin.sync_with_stdio(false);
	priority_queue<node> q;
	while (cin >> str) {
		while (q.size()) {
			q.pop();
		}
		ans = 0;
		cnt = 0;
		len = str.length();
		for (int i = 0; i < len; i++) {
			cnt += str[i] == '(';
			cnt -= (str[i] == ')' || str[i] == '?');
			if (str[i] == '?') {
				cin >> a >> b;
				q.push(make(i, b - a));
				ans += b;
				str[i] = ')';
			}
			if (cnt < 0 && (!q.size())) {
				cout << -1 << endl;
				return 0;
			}
			if (cnt < 0) {
				node now = q.top();
				q.pop();
				ans -= now.v;
				str[now.id] = '(';
				cnt += 2;
			}
		}
		if (cnt < 0) {
			cout << -1 << endl;
		}
		else {
			cout << ans << endl;
			cout << str << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <unordered_map>
#define MAXN 1010
#define MAXSIZE 10000
#define MAXVERTEX 100010
#define MAXEDGE 400010
#define ALPHASIZE 26
#define INF 999999999
#define DLEN 4
#define nil NULL
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1e9+7;
const double eps = 1e-6;

struct Node {
    int v, idx;
    bool operator > (const Node &n) const {
        return v > n.v;
    }
};

int main() {
    // freopen("input.in", "r", stdin);
    // freopen("output.out", "w", stdout);

    priority_queue<Node, vector<Node>, greater<Node> > heap;
    string str;
    int a, b, sum;
    vector<pii > dat;

    cin >> str;
    while (cin >> a >> b) {
        pii p = make_pair(a, b);
        dat.push_back(p);
        sum += b;
    }

    // cout << sum << endl;

    int mark = 0, counter = 0;
    for (int i = 0; i < str.length(); i++) {
        if (str[i] == '(') mark++;
        else {
            if (str[i] == '?') {
                str[i] = ')';
                heap.push(Node{dat[counter].first - dat[counter].second, i});
                counter++;
            }
            mark--;
        }
        if (mark < 0) {
            if (!heap.empty()) {
                Node top = heap.top();
                // cout << top.v << ' ' << top.idx << endl;
                heap.pop();
                sum += top.v;
                mark += 2;
                str[top.idx] = '(';
                // cout << sum << endl;
            } else {
                cout << -1 << endl;
                return 0;
            }
        }
    }

    if (mark == 0) {
        cout << sum << endl << str << endl;
    } else {
        cout << -1 << endl;
    }

    // fclose(stdin);
    // fclose(stdout);

    return 0;
}

/*
X0X
.0.
.X.

*/


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <unordered_map>
#define MAXN 1010
#define MAXSIZE 10000
#define MAXVERTEX 100010
#define MAXEDGE 400010
#define ALPHASIZE 26
#define INF 999999999
#define DLEN 4
#define nil NULL
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1e9+7;
const double eps = 1e-6;

struct Node {
    int v, idx;
    bool operator > (const Node &n) const {
        return v > n.v;
    }
};

int main() {
    // freopen("input.in", "r", stdin);
    // freopen("output.out", "w", stdout);

    priority_queue<Node, vector<Node>, greater<Node> > heap;
    string str;
    int a, b, sum = 0;
    vector<pii > dat;

    cin >> str;
    while (cin >> a >> b) {
        pii p = make_pair(a, b);
        dat.push_back(p);
        sum += b;
    }

    // cout << sum << endl;

    int mark = 0, counter = 0;
    for (int i = 0; i < str.length(); i++) {
        if (str[i] == '(') mark++;
        else {
            if (str[i] == '?') {
                str[i] = ')';
                heap.push(Node{dat[counter].first - dat[counter].second, i});
                counter++;
            }
            mark--;
        }
        if (mark < 0) {
            if (!heap.empty()) {
                Node top = heap.top();
                // cout << top.v << ' ' << top.idx << endl;
                heap.pop();
                sum += top.v;
                mark += 2;
                str[top.idx] = '(';
                // cout << sum << endl;
            } else {
                cout << -1 << endl;
                return 0;
            }
        }
    }

    if (mark == 0) {
        cout << sum << endl << str << endl;
    } else {
        cout << -1 << endl;
    }

    // fclose(stdin);
    // fclose(stdout);

    return 0;
}

/*
X0X
.0.
.X.

*/


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <unordered_map>
#define MAXN 1010
#define MAXSIZE 10000
#define MAXVERTEX 100010
#define MAXEDGE 400010
#define ALPHASIZE 26
#define INF 999999999
#define DLEN 4
#define nil NULL
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1e9+7;
const double eps = 1e-6;

struct Node {
    ll v, idx;
    bool operator > (const Node &n) const {
        return v > n.v;
    }
};

int main() {
    // freopen("input.in", "r", stdin);
    // freopen("output.out", "w", stdout);

    priority_queue<Node, vector<Node>, greater<Node> > heap;
    string str;
    ll a, b, sum = 0;
    vector<pii > dat;

    cin >> str;
    while (cin >> a >> b) {
        pii p = make_pair(a, b);
        dat.push_back(p);
        sum += b;
    }

    // cout << sum << endl;

    int mark = 0, counter = 0;
    for (int i = 0; i < str.length(); i++) {
        if (str[i] == '(') mark++;
        else {
            if (str[i] == '?') {
                str[i] = ')';
                heap.push(Node{dat[counter].first - dat[counter].second, i});
                counter++;
            }
            mark--;
        }
        if (mark < 0) {
            if (!heap.empty()) {
                Node top = heap.top();
                // cout << top.v << ' ' << top.idx << endl;
                heap.pop();
                sum += top.v;
                mark += 2;
                str[top.idx] = '(';
                // cout << sum << endl;
            } else {
                cout << -1 << endl;
                return 0;
            }
        }
    }

    if (mark == 0) {
        cout << sum << endl << str << endl;
    } else {
        cout << -1 << endl;
    }

    // fclose(stdin);
    // fclose(stdout);

    return 0;
}

/*
X0X
.0.
.X.

*/


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

int arr[50001];
struct node {
	int s, e, m, mn, lz = 0;
	node *l, *r;
	node (int _s, int _e): s(_s), e(_e), m((_s+_e)/2) {
		if (s == e) mn = arr[s];
		else {
			l = new node(s, m);
			r = new node(m+1, e);
			mn = min(l->mn, r->mn);
		}
	}
	void propagate () {
		l->update(s, m, lz);
		r->update(m+1, e, lz);
		lz = 0;
	}
	void update (int x, int y, int v) {
		if (x <= s && y >= e) {
			mn += v, lz += v;
			return;
		}
		if (lz) propagate();
		if (x <= m) l->update(x, y, v);
		if (y >= m+1) r->update(x, y, v);
		mn = min(l->mn, r->mn);
	}
	int query (int x, int y) {
		if (x <= s && y >= e) return mn;
		if (lz) propagate();
		int ret = 1e9;
		if (x <= m) ret = min(ret, l->query(x, y));
		if (y >= m+1) ret = min(ret, r->query(x, y));
		return ret;
	}
};
int main(){
	ios::sync_with_stdio(0);
	string s;
	ll q = 0, n, cost[50001][2], posi[50001], cnt = 0, out = 0;
	vector<pair<ll, ll> > vec;
	cin >> s;
	n = s.size();
	for (int i = 0; i < n; i++) {
		if (s[i] != ')') arr[i] = (i ? arr[i-1] : 0) + 1;
		else arr[i] = (i ? arr[i-1] : 0) - 1;
		if (s[i] == '?') posi[q++] = i;
	}
	for (int i = 0; i < q; i++) {
		cin >> cost[i][0] >> cost[i][1];
		out += cost[i][0];
		vec.push_back({cost[i][1] - cost[i][0], posi[i]});
	}
	sort(vec.begin(), vec.end());
	node *root = new node(0, n - 1);
	if (root->query(0, n - 1) < 0) {
		cout << -1;
		return 0;
	}

	bool open[50001];
	fill(open, open + q, true);
	while (root->query(n-1, n-1) > 0 && cnt < q) {
		int curcost, ind;
		tie(curcost, ind) = vec[cnt++];
		if (root->query(ind, n-1) < 2) continue;
		root->update(ind, n-1, -2);
		out += curcost;
		open[cnt-1] = false;
	}
	if (root->query(n-1, n-1) != 0) cout << -1;
	else {
		cout << out << '\n';
		for (int i = 0; i < q; i++) s[posi[i]] = open[i] ? '(' : ')';
		cout << s;
	}
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

int arr[50001];
struct node {
	int s, e, m, mn, lz = 0;
	node *l, *r;
	node (int _s, int _e): s(_s), e(_e), m((_s+_e)/2) {
		if (s == e) mn = arr[s];
		else {
			l = new node(s, m);
			r = new node(m+1, e);
			mn = min(l->mn, r->mn);
		}
	}
	void propagate () {
		l->update(s, m, lz);
		r->update(m+1, e, lz);
		lz = 0;
	}
	void update (int x, int y, int v) {
		if (x <= s && y >= e) {
			mn += v, lz += v;
			return;
		}
		if (lz) propagate();
		if (x <= m) l->update(x, y, v);
		if (y >= m+1) r->update(x, y, v);
		mn = min(l->mn, r->mn);
	}
	int query (int x, int y) {
		if (x <= s && y >= e) return mn;
		if (lz) propagate();
		int ret = 1e9;
		if (x <= m) ret = min(ret, l->query(x, y));
		if (y >= m+1) ret = min(ret, r->query(x, y));
		return ret;
	}
};
int main(){
	ios::sync_with_stdio(0);
	string s;
	ll q = 0, n, cost[50001][2], posi[50001], cnt = 0, out = 0;
	vector<pair<ll, ll> > vec;
	cin >> s;
	n = s.size();
	for (int i = 0; i < n; i++) {
		if (s[i] != ')') arr[i] = (i ? arr[i-1] : 0) + 1;
		else arr[i] = (i ? arr[i-1] : 0) - 1;
		if (s[i] == '?') posi[q++] = i, s[i] = '(';
	}
	for (int i = 0; i < q; i++) {
		cin >> cost[i][0] >> cost[i][1];
		out += cost[i][0];
		vec.push_back({cost[i][1] - cost[i][0], posi[i]});
	}
	sort(vec.begin(), vec.end());
	node *root = new node(0, n - 1);
	if (root->query(0, n - 1) < 0) {
		cout << -1;
		return 0;
	}

	while (root->query(n-1, n-1) > 0 && cnt < q) {
		int curcost, ind;
		tie(curcost, ind) = vec[cnt++];
		if (root->query(ind, n-1) < 2) continue;
		root->update(ind, n-1, -2);
		out += curcost;
		s[ind] = ')';
	}
	if (root->query(n-1, n-1) != 0) cout << -1;
	else {
		cout << out << '\n' << s;
	}
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

int arr[50001];
struct node {
	int s, e, m, mn, lz = 0;
	node *l, *r;
	node (int _s, int _e): s(_s), e(_e), m((_s+_e)/2) {
		if (s == e) mn = arr[s];
		else {
			l = new node(s, m);
			r = new node(m+1, e);
			mn = min(l->mn, r->mn);
		}
	}
	void propagate () {
		l->update(s, m, lz);
		r->update(m+1, e, lz);
		lz = 0;
	}
	void update (int x, int y, int v) {
		if (x <= s && y >= e) {
			mn += v, lz += v;
			return;
		}
		if (lz) propagate();
		if (x <= m) l->update(x, y, v);
		if (y >= m+1) r->update(x, y, v);
		mn = min(l->mn, r->mn);
	}
	int query (int x, int y) {
		if (x <= s && y >= e) return mn;
		if (lz) propagate();
		int ret = 1e9;
		if (x <= m) ret = min(ret, l->query(x, y));
		if (y >= m+1) ret = min(ret, r->query(x, y));
		return ret;
	}
};
int main(){
	ios::sync_with_stdio(0);
	string s;
	ll q = 0, n, cost[50001][2], posi[50001], cnt = 0, out = 0;
	vector<pair<ll, ll> > vec;
	cin >> s;
	n = s.size();
	for (int i = 0; i < n; i++) {
		if (s[i] != ')') arr[i] = (i ? arr[i-1] : 0) + 1;
		else arr[i] = (i ? arr[i-1] : 0) - 1;
		if (s[i] == '?') posi[q++] = i;
	}
	for (int i = 0; i < q; i++) {
		cin >> cost[i][0] >> cost[i][1];
		out += cost[i][0];
		vec.push_back({cost[i][1] - cost[i][0], posi[i]});
	}
	sort(vec.begin(), vec.end());
	node *root = new node(0, n - 1);
	if (root->query(0, n - 1) < 0) {
		cout << -1;
		return 0;
	}

	bool open[50001];
	fill(open, open + q, true);
	while (root->query(n-1, n-1) > 0 && cnt < q) {
		int curcost, ind;
		tie(curcost, ind) = vec[cnt++];
		if (root->query(ind, n-1) < 2) continue;
		root->update(ind, n-1, -2);
		out += curcost;
		open[ind] = false;
	}
	if (root->query(n-1, n-1) != 0) cout << -1;
	else {
		cout << out << '\n';
		for (int i = 0; i < q; i++) s[posi[i]] = open[i] ? '(' : ')';
		cout << s;
	}
}


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

struct Node
{
	int left;
	int right;
	friend bool operator < (const Node& A, const Node& B)
	{
		return (A.left - A.right) > (B.left - B.right);
	}
};

int main()
{
	string str;
	while (cin >> str)
	{
		int cnt = 0;
		for (size_t i = 0; i < str.length(); ++i)
		{
			if (str[i] == '?')
				++cnt;
		}

		vector<Node> vec;
		Node tmp;
		for (size_t i = 0; i < cnt; ++i)
		{
			cin >> tmp.left >> tmp.right;
			vec.push_back(tmp);
		}

		int res = 0, sum = 0;
		cnt = -1;
		priority_queue<Node> pq;	//increase order

		for (size_t i = 0; i < str.length(); ++i)
		{
			if (str[i] == '(')
				sum += 1;
			else if (str[i] == ')')
				sum -= 1;
			else
			{
				++cnt;
				pq.push(vec[cnt]);
				res += vec[cnt].right;
				sum -= 1;
				str[i] = ')';
			}

			if (sum < 0 && pq.empty())
                break;
            else if (sum < 0)
			{
                Node tmp = pq.top();
                pq.pop();
                res += tmp.left - tmp.right;
                str[i] = '(';
                sum += 2;
			}
		}

		if (sum != 0)
			res = -1;

		cout << res << endl;
		if (res != -1)
			cout << str << endl;
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

typedef long long llong;

struct Node
{
    int id;
	int left;
	int right;
	friend bool operator < (const Node& A, const Node& B)
	{
		return (A.left - A.right) > (B.left - B.right);
	}
};

int main()
{
	string str;
	while (cin >> str)
	{
	    vector<int> id;
		int cnt = 0;
		for (size_t i = 0; i < str.length(); ++i)
		{
			if (str[i] == '?')
				{
				    ++cnt;
				    id.push_back(i);
				}
		}

		vector<Node> vec;
		Node tmp;
		for (size_t i = 0; i < cnt; ++i)
		{
			cin >> tmp.left >> tmp.right;
			tmp.id = i;
			vec.push_back(tmp);
		}

		int sum = 0;
		llong res = 0;
		cnt = -1;
		priority_queue<Node> pq;	//increase order

		for (size_t i = 0; i < str.length(); ++i)
		{
			if (str[i] == '(')
				sum += 1;
			else if (str[i] == ')')
				sum -= 1;
			else
			{
				++cnt;
				pq.push(vec[cnt]);
				res += vec[cnt].right;
				sum -= 1;
				str[i] = ')';
			}

			if (sum < 0 && pq.empty())
                break;
            else if (sum < 0)
			{
                Node tmp = pq.top();
                pq.pop();
                res += tmp.left - tmp.right;
                str[id[tmp.id]] = '(';
                sum += 2;
			}
		}

		if (sum != 0)
			res = -1;

		cout << res << endl;
		if (res != -1)
			cout << str << endl;
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

struct Node
{
    int id;
	int left;
	int right;
	friend bool operator < (const Node& A, const Node& B)
	{
		return (A.left - A.right) > (B.left - B.right);
	}
};

int main()
{
	string str;
	while (cin >> str)
	{
	    vector<int> id;
		int cnt = 0;
		for (size_t i = 0; i < str.length(); ++i)
		{
			if (str[i] == '?')
				{
				    ++cnt;
				    id.push_back(i);
				}
		}

		vector<Node> vec;
		Node tmp;
		for (size_t i = 0; i < cnt; ++i)
		{
			cin >> tmp.left >> tmp.right;
			tmp.id = i;
			vec.push_back(tmp);
		}

		int res = 0, sum = 0;
		cnt = -1;
		priority_queue<Node> pq;	//increase order

		for (size_t i = 0; i < str.length(); ++i)
		{
			if (str[i] == '(')
				sum += 1;
			else if (str[i] == ')')
				sum -= 1;
			else
			{
				++cnt;
				pq.push(vec[cnt]);
				res += vec[cnt].right;
				sum -= 1;
				str[i] = ')';
			}

			if (sum < 0 && pq.empty())
                break;
            else if (sum < 0)
			{
                Node tmp = pq.top();
                pq.pop();
                res += tmp.left - tmp.right;
                str[id[tmp.id]] = '(';
                sum += 2;
			}
		}

		if (sum != 0)
			res = -1;

		cout << res << endl;
		if (res != -1)
			cout << str << endl;
	}
	return 0;
}


#define _HAS_ITERATOR_DEBUGGING 0
#define _ITERATOR_DEBUG_LEVEL 0
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>
#include <algorithm>
#include <functional>
#include <cstring>
#include <map>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <list>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>

#define setzero(arr) memset(arr, 0, sizeof(arr))

int ___read_tmp;
#define fi(i, from, to) for(int i = (from); i < (to); i++)
#define fri(i, from, to) for(int i = (from); i >= (to); i--)
#define fa(it, col) for(auto (it) = (col).begin(); (it) != (col).end(); (it)++)
#define fra(it, col) for(auto (it) = (col).rbegin(); (it) != (col).rend(); (it)++)
#define getBit(vv, bit) ((vv & (lol)(1LL << (31 - bit))) == 0LL ? 0 : 1)
#define getBit64(vv, bit) ((vv & (lol)(1LL << (63 - bit))) == 0LL ? 0 : 1)
#define vecits(obj) obj.begin(), obj.end()
#define strip_string(str) (str)[strcspn((str), "\r\n")] = 0;
#define read_int() (scanf("%d ", &___read_tmp), ___read_tmp)
#define MOD 1000000007
#define PI 3.14159265
#define INF 10000000
//#define FILE_IO "peacefulsets"

typedef unsigned long long lol;
typedef  long long slol;
using namespace std;

char seq[50005];
pair<int, int> costs[50005];
set<pair<int, int>> ss;

int main() {

#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#elif defined(FILE_IO)
	freopen(FILE_IO ".in", "r", stdin);
	freopen(FILE_IO ".out", "w", stdout);
#endif

	scanf("%s", seq);
	int len = strlen(seq);
	int sum = 0, n = 0;

	fi(i, 0, len)
	{
		if (seq[i] == '?')
		{
			n++;
			scanf("%d %d", &costs[i].first, &costs[i].second);
			ss.insert(make_pair(costs[i].first - costs[i].second, i));
		}
		else if (seq[i] == '(')
		{
			sum++;
		}
		else
		{
			sum--;
		}
	}

	int b = (n + sum);
	if (b % 2 == 1)
	{
		printf("-1");
		return 0;
	}
	b /= 2;

	int a = -sum + b;
	if (a < 0 || b < 0)
	{
		printf("-1");
		return 0;
	}

	set<pair<int, int>> fw_opn;
	int opn = 0;
	for (auto it = ss.begin(); it != ss.end(); it++)
	{
		if (opn < a)
		{
			opn++;
			seq[it->second] = '(';
			fw_opn.insert(make_pair(it->first, it->second));
		}
		else
		{
			seq[it->second] = ')';
		}
	}

	set<pair<int, int>> been_cls;
	sum = 0;
	fi(i, 0, len)
	{
		if (seq[i] == '(')
		{
			sum++;
			fw_opn.erase(make_pair(costs[i].first - costs[i].second, i));
		}
		else
		{
			if (costs[i].first != 0)
			{
				been_cls.insert(make_pair(costs[i].first - costs[i].second, i));
			}

			sum--;
			if (sum == -1)
			{
				if (been_cls.size() == 0 || fw_opn.size() == 0)
				{
					printf("-1");
					return 0;
				}

				auto left2 =*been_cls.rbegin();
				auto right = fw_opn.begin();

				seq[left2.second] = '(';
				seq[right->second] = ')';

				been_cls.erase(left2);
				fw_opn.erase(right);
			}
		}
	}

	lol cost = 0;
	fi(i, 0, len)
	{
		if (seq[i] == '(')
			cost += costs[i].first;
		else
			cost += costs[i].second;
	}

	printf("%I64d\n", cost);
	printf("%s", seq);

	return 0;
}


#define _HAS_ITERATOR_DEBUGGING 0
#define _ITERATOR_DEBUG_LEVEL 0
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>
#include <algorithm>
#include <functional>
#include <cstring>
#include <map>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <list>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>

#define setzero(arr) memset(arr, 0, sizeof(arr))

int ___read_tmp;
#define fi(i, from, to) for(int i = (from); i < (to); i++)
#define fri(i, from, to) for(int i = (from); i >= (to); i--)
#define fa(it, col) for(auto (it) = (col).begin(); (it) != (col).end(); (it)++)
#define fra(it, col) for(auto (it) = (col).rbegin(); (it) != (col).rend(); (it)++)
#define getBit(vv, bit) ((vv & (lol)(1LL << (31 - bit))) == 0LL ? 0 : 1)
#define getBit64(vv, bit) ((vv & (lol)(1LL << (63 - bit))) == 0LL ? 0 : 1)
#define vecits(obj) obj.begin(), obj.end()
#define strip_string(str) (str)[strcspn((str), "\r\n")] = 0;
#define read_int() (scanf("%d ", &___read_tmp), ___read_tmp)
#define MOD 1000000007
#define PI 3.14159265
#define INF 10000000
//#define FILE_IO "peacefulsets"

typedef unsigned long long lol;
typedef  long long slol;
using namespace std;

char seq[50005];
pair<int, int> costs[50005];
set<pair<int, int>> ss;

int main() {

#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#elif defined(FILE_IO)
	freopen(FILE_IO ".in", "r", stdin);
	freopen(FILE_IO ".out", "w", stdout);
#endif

	scanf("%s", seq);
	int len = strlen(seq);
	int sum = 0, n = 0;

	fi(i, 0, len)
	{
		if (seq[i] == '?')
		{
			n++;
			scanf("%d %d", &costs[i].first, &costs[i].second);
			ss.insert(make_pair(costs[i].first - costs[i].second, i));
		}
		else if (seq[i] == '(')
		{
			sum++;
		}
		else
		{
			sum--;
		}
	}

	int b = (n + sum);
	if (b % 2 == 1)
	{
		printf("-1");
		return 0;
	}
	b /= 2;

	int a = -sum + b;
	if (a < 0 || b < 0)
	{
		printf("-1");
		return 0;
	}

	set<pair<int, int>> fw_opn;
	int opn = 0;
	for (auto it = ss.begin(); it != ss.end(); it++)
	{
		if (opn < a)
		{
			opn++;
			seq[it->second] = '(';
			fw_opn.insert(make_pair(it->first, it->second));
		}
		else
		{
			seq[it->second] = ')';
		}
	}

	set<pair<int, int>> been_cls;
	sum = 0;
	fi(i, 0, len)
	{
		if (seq[i] == '(')
		{
			sum++;
			fw_opn.erase(make_pair(costs[i].first - costs[i].second, i));
		}
		else
		{
			if (costs[i].first != 0)
			{
				been_cls.insert(make_pair(costs[i].first - costs[i].second, i));
			}

			sum--;
			if (sum == -1)
			{
				if (been_cls.size() == 0 || fw_opn.size() == 0)
				{
					printf("-1");
					return 0;
				}

				auto left2 =*been_cls.begin();
				auto right = *fw_opn.rbegin();

				seq[left2.second] = '(';
				seq[right.second] = ')';

				been_cls.erase(left2);
				fw_opn.erase(right);
			}
		}
	}

	lol cost = 0;
	fi(i, 0, len)
	{
		if (seq[i] == '(')
			cost += costs[i].first;
		else
			cost += costs[i].second;
	}

	printf("%I64d\n", cost);
	printf("%s", seq);

	return 0;
}


#define _HAS_ITERATOR_DEBUGGING 0
#define _ITERATOR_DEBUG_LEVEL 0
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>
#include <algorithm>
#include <functional>
#include <cstring>
#include <map>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <list>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>

#define setzero(arr) memset(arr, 0, sizeof(arr))

int ___read_tmp;
#define fi(i, from, to) for(int i = (from); i < (to); i++)
#define fri(i, from, to) for(int i = (from); i >= (to); i--)
#define fa(it, col) for(auto (it) = (col).begin(); (it) != (col).end(); (it)++)
#define fra(it, col) for(auto (it) = (col).rbegin(); (it) != (col).rend(); (it)++)
#define getBit(vv, bit) ((vv & (lol)(1LL << (31 - bit))) == 0LL ? 0 : 1)
#define getBit64(vv, bit) ((vv & (lol)(1LL << (63 - bit))) == 0LL ? 0 : 1)
#define vecits(obj) obj.begin(), obj.end()
#define strip_string(str) (str)[strcspn((str), "\r\n")] = 0;
#define read_int() (scanf("%d ", &___read_tmp), ___read_tmp)
#define MOD 1000000007
#define PI 3.14159265
#define INF 10000000
//#define FILE_IO "peacefulsets"

typedef unsigned long long lol;
typedef  long long slol;
using namespace std;

char seq[50005];
pair<int, int> costs[50005];
set<pair<int, int>> ss;

int main() {

#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#elif defined(FILE_IO)
	freopen(FILE_IO ".in", "r", stdin);
	freopen(FILE_IO ".out", "w", stdout);
#endif

	scanf("%s", seq);
	int len = strlen(seq);
	int sum = 0, n = 0;

	fi(i, 0, len)
	{
		if (seq[i] == '?')
		{
			n++;
			scanf("%d %d", &costs[i].first, &costs[i].second);
			ss.insert(make_pair(costs[i].first - costs[i].second, i));
		}
		else if (seq[i] == '(')
		{
			sum++;
		}
		else
		{
			sum--;
		}
	}

	int b = (n + sum);
	if (b % 2 == 1)
	{
		printf("-1");
		return 0;
	}
	b /= 2;

	int a = -sum + b;
	if (a < 0 || b < 0)
	{
		printf("-1");
		return 0;
	}

	set<pair<int, int>> fw_opn;
	int opn = 0;
	for (auto it = ss.begin(); it != ss.end(); it++)
	{
		if (opn < a)
		{
			opn++;
			seq[it->second] = '(';
			fw_opn.insert(make_pair(it->first, it->second));
		}
		else
		{
			seq[it->second] = ')';
		}
	}

	set<pair<int, int>> been_cls;
	sum = 0;
	fi(i, 0, len)
	{
		if (seq[i] == '(')
		{
			sum++;
			if (costs[i].first != 0)
			{
				fw_opn.erase(make_pair(costs[i].first - costs[i].second, i));
			}
		}
		else
		{
			if (costs[i].first != 0)
			{
				been_cls.insert(make_pair(costs[i].first - costs[i].second, i));
			}

			sum--;
			if (sum == -1)
			{
				if (been_cls.size() == 0 || fw_opn.size() == 0)
				{
					printf("-1");
					return 0;
				}

				auto left2 =*been_cls.begin();
				auto right = *fw_opn.rbegin();

				seq[left2.second] = '(';
				seq[right.second] = ')';

				been_cls.erase(left2);
				fw_opn.erase(right);

				sum+=2;
			}
		}
	}

	lol cost = 0;
	fi(i, 0, len)
	{
		if (seq[i] == '(')
			cost += costs[i].first;
		else
			cost += costs[i].second;
	}

	printf("%I64d\n", cost);
	printf("%s", seq);

	return 0;
}


#include <bits/stdc++.h>
#define forn(x, n)              for (ll x = 0; x < n; x++)
#define forr(x, n)              for (int x = n; x >= 0; x--)
#define fore(x, a, b, c)        for (int x = a; x <= b; x += c)
#define forre(x, a, b, c)       for (int x = a; x >= b; x -= c)
#define clr                     cin.ignore(numeric_limits<streamsize>::max(), '\n')
#define all(container)          (container).begin(), (container).end()
#define rall(container)         (container).rbegin(), (container).rend()
#define FILL(x, val)            memset(x, val, sizeof(x))
#define pb                      push_back
#define eb                      emplace_back
#define fi                      first
#define se                      second
#define mod                     (int)1000000007
#define INF                     (int)0x3f3f3f3f

using namespace std;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s;
    cin >> s;
    ll n = s.size(), o = 0, cost = 0;
    vector<ll> op(n, 0);
    vector<ll> cl(n, 0);
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> check;
    forn(i, (ll) s.size()) {
        if (s[i] == '?')
            cin >> op[i] >> cl[i];
    }
    forn(i, n) {
        if (s[i] == '(')
            ++o;
        else if (s[i] == ')')
            --o;
        else {
            s[i] = ')';
            cost += cl[i];
            check.push({op[i] - cl[i], i});
            --o;
        }
        if (o < 0) {
            if (check.empty())
                continue;
            pair<ll, ll> temp = check.top();
            check.pop();
            cost += temp.first;
            //cerr << temp.first << " " << temp.second << '\n';
            o += 2;
            s[temp.second] = '(';
        }
    }
    // pair<int, int> temp = check.top();
    // cerr << temp.first << " " << temp.second << '\n';
    if (o)
        cout << -1 << '\n';
    else
        cout << cost << '\n' << s << '\n';
    return 0;
}


#include <bits/stdc++.h>
#define forn(x, n)              for (ll x = 0; x < n; x++)
#define forr(x, n)              for (int x = n; x >= 0; x--)
#define fore(x, a, b, c)        for (int x = a; x <= b; x += c)
#define forre(x, a, b, c)       for (int x = a; x >= b; x -= c)
#define clr                     cin.ignore(numeric_limits<streamsize>::max(), '\n')
#define all(container)          (container).begin(), (container).end()
#define rall(container)         (container).rbegin(), (container).rend()
#define FILL(x, val)            memset(x, val, sizeof(x))
#define pb                      push_back
#define eb                      emplace_back
#define fi                      first
#define se                      second
#define mod                     (int)1000000007
#define INF                     (int)0x3f3f3f3f

using namespace std;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s;
    cin >> s;
    ll n = s.size(), o = 0, cost = 0;
    vector<ll> op(n, 0);
    vector<ll> cl(n, 0);
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> check;
    forn(i, (ll) s.size()) {
        if (s[i] == '?')
            cin >> op[i] >> cl[i];
    }
    bool ok = true;
    forn(i, n) {
        if (s[i] == '(')
            ++o;
        else if (s[i] == ')')
            --o;
        else {
            s[i] = ')';
            cost += cl[i];
            check.push({op[i] - cl[i], i});
            --o;
        }
        if (o < 0) {
            if (check.empty())
                break;
            pair<ll, ll> temp = check.top();
            check.pop();
            cost += temp.first;
            //cerr << temp.first << " " << temp.second << '\n';
            o += 2;
            s[temp.second] = '(';
        }
        if (o < 0) {
            ok = false;
            break;
        }
    }
    // pair<int, int> temp = check.top();
    // cerr << temp.first << " " << temp.second << '\n';
    if (o || !ok)
        cout << -1 << '\n';
    else
        cout << cost << '\n' << s << '\n';
    return 0;
}


#include <bits/stdc++.h>
#define forn(x, n)              for (int x = 0; x < n; x++)
#define forr(x, n)              for (int x = n; x >= 0; x--)
#define fore(x, a, b, c)        for (int x = a; x <= b; x += c)
#define forre(x, a, b, c)       for (int x = a; x >= b; x -= c)
#define clr                     cin.ignore(numeric_limits<streamsize>::max(), '\n')
#define all(container)          (container).begin(), (container).end()
#define rall(container)         (container).rbegin(), (container).rend()
#define FILL(x, val)            memset(x, val, sizeof(x))
#define pb                      push_back
#define eb                      emplace_back
#define fi                      first
#define se                      second
#define mod                     (int)1000000007
#define INF                     (int)0x3f3f3f3f

using namespace std;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s;
    cin >> s;
    int n = s.size(), o = 0, cost = 0;
    vector<int> op(n, 0);
    vector<int> cl(n, 0);
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> check;
    forn(i, (int) s.size()) {
        if (s[i] == '?')
            cin >> op[i] >> cl[i];
    }
    forn(i, n) {
        if (s[i] == '(')
            ++o;
        else if (s[i] == ')')
            --o;
        else {
            s[i] = ')';
            cost += cl[i];
            check.push({op[i] - cl[i], i});
            --o;
        }
        if (o < 0) {
            if (check.empty())
                continue;
            pair<int, int> temp = check.top();
            check.pop();
            cost += temp.first;
            //cerr << temp.first << " " << temp.second << '\n';
            o += 2;
            s[temp.second] = '(';
        }
    }
    // pair<int, int> temp = check.top();
    // cerr << temp.first << " " << temp.second << '\n';
    if (o)
        cout << -1 << '\n';
    else
        cout << cost << '\n' << s << '\n';
    return 0;
}


#include <bits/stdc++.h>
#define forn(x, n)              for (ll x = 0; x < n; x++)
#define forr(x, n)              for (int x = n; x >= 0; x--)
#define fore(x, a, b, c)        for (int x = a; x <= b; x += c)
#define forre(x, a, b, c)       for (int x = a; x >= b; x -= c)
#define clr                     cin.ignore(numeric_limits<streamsize>::max(), '\n')
#define all(container)          (container).begin(), (container).end()
#define rall(container)         (container).rbegin(), (container).rend()
#define FILL(x, val)            memset(x, val, sizeof(x))
#define pb                      push_back
#define eb                      emplace_back
#define fi                      first
#define se                      second
#define mod                     (int)1000000007
#define INF                     (int)0x3f3f3f3f

using namespace std;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s;
    cin >> s;
    ll n = s.size(), o = 0, cost = 0;
    vector<ll> op(n, 0);
    vector<ll> cl(n, 0);
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> check;
    forn(i, (ll) s.size()) {
        if (s[i] == '?')
            cin >> op[i] >> cl[i];
    }
    bool ok = true;
    forn(i, n) {
        if (s[i] == '(')
            ++o;
        else if (s[i] == ')')
            --o;
        else {
            s[i] = ')';
            cost += cl[i];
            check.push({op[i] - cl[i], i});
            --o;
        }
        if (o < 0) {
            if (check.empty())
                break;
            pair<ll, ll> temp = check.top();
            check.pop();
            cost += temp.first;
            //cerr << temp.first << " " << temp.second << '\n';
            o += 2;
            s[temp.second] = '(';
        }
        if (o < 0) {
            ok = false;
            break;
        }
    }
    // pair<int, int> temp = check.top();
    // cerr << temp.first << " " << temp.second << '\n';
    if (o || cost < 0 || !ok)
        cout << -1 << '\n';
    else
        cout << cost << '\n' << s << '\n';
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

string s;
vector <int> v ,v1;
long long b,c,l,r,siz;

void ssort(int l, int r)
{
  int i=l,j=r,x=v[(i+j)/2];
  while (i<=j)
  {
    while (v[i]>x) i++;
    while (v[j]<x) j--;
    if (i<=j) {
               swap(v[i],v[j]);
               swap(v1[i],v1[j]);
               i++;
               j--;
               }
  }
  if (i<r) ssort(i,r);
  if (j>l) ssort(l,j);
}
int main()
{
  cin>>s;
	for (int i=0;i<s.size();i++)
	{
		if (s[i]=='(') {  b++; continue;}
		if (s[i]==')') {  b--; continue;}
		  cin>>l>>r;
		      b--;
		      s[i]=')';
		      c+=r;
		     v.push_back(l-r);
		     v1.push_back(i);
		     siz++;
		       }
		ssort(0,siz-1);
		while (b<0 && siz>=0)
		{
		b+=2;
		s[v1[siz-1]-1]='(';
		c+=v[siz-1];
		siz--;
		if (b>0) break;
	}
	if (b!=0) {cout<<-1<<endl;
	           return 0;
	        }
	 cout<<c<<endl;
	 cout<<s<<endl;

}


#include<bits/stdc++.h>
using namespace std;
string s;
set <pair<int,int> > v;
long long b,c,l,r;
int main()
{
	cin>>s;
	for (int i=0;i<s.size();i++)
	{
		if (s[i]=='(') b++;
		else if (s[i]==')') b--;
		else {
		    cin>>l>>r;
		    b--;
		    s[i]=')';
		    c+=r;
		    v.insert({l-r,i});
		}
		while (b<0 && v.size()!=0 )
		{
		 b+=2;
		 s[(*v.begin()).second]='(';
		 c+=(*v.begin()).first;
		 v.erase(v.begin());
		if (b<0) break;
		}
	}
	int bal=0;
	for (int i=0;i<s.size();i++)
	 {
	   if (s[i]=='(') bal++;
	    else bal--;
	    if (bal<0) break;
	 }
	 if (bal!=0) cout<<-1<<endl;
	else { cout<<c<<endl;
	       cout<<s<<endl;
	     }
}


#include<bits/stdc++.h>
using namespace std;
string s;
vector <int> v ,v1;
long long b,c,l,r,siz=0;

void ssort(int l, int r)
{
  int i=l,j=r,x=v[(i+j)/2];
  while (i<=j)
  {
    while (v[i]>x) i++;
    while (v[j]<x) j--;
    if (i<=j ) {
               swap(v[i],v[j]);
               swap(v1[i],v1[j]);
               i++;
               j--;
               }
  }
  if (i<r) ssort(i,r);
  if (j>l) ssort(l,j);
}
int main()
{
   // freopen("input.txt", "r", stdin);
  //   freopen("output.txt", "w", stdout);
  cin>>s;
	for (int i=0;i<s.size();i++)
	{
		if (s[i]=='(')   b++;
		else
		if (s[i]==')')   b--;
		else {
		  cin>>l>>r;
		      b--;
		      s[i]=')';
		      c+=r;
		     v.push_back(l-r);
		     v1.push_back(i);
		     siz++;
		}
    }
        ssort(0,siz);
		//for (int i=0;i<=siz;i++)
         // cout<<v[i]<<" "<<v1[i]<<endl;
		while (b<0 && siz>0)
		{
		b+=2;
		if (b>0) break;
		if (siz==0) break;
		s[v1[siz-1]]='(';
		c+=v[siz-1];
		siz--;

	}
	if (b!=0) {cout<<-1<<endl;
	           return 0;
	           }
	 cout<<c<<endl;
	 cout<<s<<endl;

}


#include<bits/stdc++.h>
using namespace std;

string s;
vector <int> v ,v1;
long long b,c,l,r,siz;
int main()
{
  cin>>s;
	for (int i=0;i<s.size();i++)
	{
		if (s[i]=='(') {  b++; continue;}
		if (s[i]==')') {  b--; continue;}
		  cin>>l>>r;
		      b--;
		      s[i]=')';
		      c+=r;
		     v.push_back(l-r);
		     v1.push_back(i);
		     siz++;
		       }
		while (b<0 && siz>=0)
		{
		b+=2;
		s[v1[siz-1]]='(';
		c+=v[siz-1];
		siz--;
	}
	if (b!=0) {cout<<-1<<endl;
	           return 0;
	        }
	 cout<<c<<endl;
	 cout<<s<<endl;

}


#include<bits/stdc++.h>
using namespace std;

string s;
vector <int> v ,v1;
long long b,c,l,r,siz;
int main()
{
  cin>>s;
	for (int i=0;i<s.size();i++)
	{
		if (s[i]=='(') {  b++; continue;}
		if (s[i]==')') {  b--; continue;}
		  cin>>l>>r;
		      b--;
		      s[i]=')';
		      c+=r;
		     v.push_back(l-r);
		     v1.push_back(i);
		     siz++;
		       }
		while (b<0 && siz>=0)
		{
		b+=2;
		s[v1[siz-1]-1]='(';
		c+=v[siz-1];
		siz--;
		if (b>0) break;
	}
	if (b!=0) {cout<<-1<<endl;
	           return 0;
	        }
	 cout<<c<<endl;
	 cout<<s<<endl;

}


#include<bits/stdc++.h>
using namespace std;

string s;
vector <int> v ,v1;
long long b,c,l,r,siz;
int main()
{
  cin>>s;
	for (int i=0;i<s.size();i++)
	{
		if (s[i]=='(') {  b++; continue;}
		if (s[i]==')') {  b--; continue;}
		  cin>>l>>r;
		      b--;
		      s[i]=')';
		      c+=r;
		     v.push_back(l-r);
		     v1.push_back(i);
		     siz++;
		       }
		while (b<0 && siz>=0)
		{
		b+=2;
		s[v1[siz-1]-1]='(';
		c+=v[siz-1];
		siz--;
		if (b<0) break;
	}
	if (b!=0) {cout<<-1<<endl;
	           return 0;
	        }
	 cout<<c<<endl;
	 cout<<s<<endl;

}


#include<bits/stdc++.h>
using namespace std;
string s;
vector <int> v ,v1;
long long b,c,l,r,siz=0;

void ssort(int l, int r)
{
  int i=l,j=r,x=v[(i+j)/2];
  while (i<=j)
  {
    while (v[i]>x) i++;
    while (v[j]<x) j--;
    if (i<=j ) {
               swap(v[i],v[j]);
               swap(v1[i],v1[j]);
               i++;
               j--;
               }
  }
  if (i<r) ssort(i,r);
  if (j>l) ssort(l,j);
}
int main()
{
   // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
  cin>>s;
	for (int i=0;i<s.size();i++)
	{
		if (s[i]=='(')   b++;
		else
		if (s[i]==')')   b--;
		else {
		  cin>>l>>r;
		      b--;
		      s[i]=')';
		      c+=r;
		     v.push_back(l-r);
		     v1.push_back(i);
		     siz++;
		}
    }
        ssort(0,siz);
		//for (int i=0;i<=siz;i++)
         // cout<<v[i]<<" "<<v1[i]<<endl;
		while (b<0 && siz>0)
		{
       if (v1[siz-1]==s.size()-1) {siz--; continue;}
		b+=2;
		s[v1[siz-1]]='(';
		c+=v[siz-1];
		siz--;
		if (b>0) break;
		if (siz==0) break;
	}
	if (b!=0) {cout<<-1<<endl;
	           return 0;
	           }
	 cout<<c<<endl;
	 cout<<s<<endl;

}


#include<bits/stdc++.h>
using namespace std;
string s;
vector <int> v ,v1;
long long b,c,l,r,siz=-1;

void ssort(int l, int r)
{
  int i=l,j=r,x=v[(i+j)/2];
  while (i<=j)
  {
    while (v[i]>x) i++;
    while (v[j]<x) j--;
    if (i<=j ) {
               swap(v[i],v[j]);
               swap(v1[i],v1[j]);
               i++;
               j--;
               }
  }
  if (i<r) ssort(i,r);
  if (j>l) ssort(l,j);
}
int main()
{
    //freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
  cin>>s;
	for (int i=0;i<s.size();i++)
	{
		if (s[i]=='(')   b++;
		else
		if (s[i]==')')   b--;
		else {
		  cin>>l>>r;
		      b--;
		      s[i]=')';
		      c+=r;
		     v.push_back(l-r);
		     v1.push_back(i);
		     siz++;
		}
    }
        ssort(0,siz);
		for (int i=0;i<=siz;i++)
            cout<<v[i]<<" "<<v1[i]<<endl;

		while (b<0 && siz>0)
		{
		b+=2;
		s[v1[siz]]='(';
		c+=v[siz];
		siz--;
		if (b>=0) break;
		if (siz==0) break;
	}
	if (b!=0) {cout<<-1<<endl;
	           return 0;
	        }
	 cout<<c<<endl;
	 cout<<s<<endl;

}


#include<bits/stdc++.h>
using namespace std;
string s;
set <pair<int,int> > v;
long long b,c,l,r;
int main()
{
	cin>>s;
	for (int i=0;i<s.size();i++)
	{
		if (s[i]=='(') b++;
		else if (s[i]==')') b--;
		else {
		    cin>>l>>r;
		    b--;
		    s[i]=')';
		    c+=r;
		    v.insert({l-r,i});
		}
		while (b<0 && v.size()!=0 )
		{
		 b+=2;
		 s[(*v.begin()).second]='(';
		 c+=(*v.begin()).first;
		 v.erase(v.begin());
		if (b<0) break;
		}
	}
	if (b!=0) cout<<-1<<endl;
	else { cout<<c<<endl;
	       cout<<s<<endl;
	     }
}


#include<bits/stdc++.h>
using namespace std;
string s;
vector <int> v ,v1;
long long b,c,l,r,siz=0;

void ssort(int l, int r)
{
  int i=l,j=r,x=v[(i+j)/2];
  while (i<=j)
  {
    while (v[i]>x) i++;
    while (v[j]<x) j--;
    if (i<=j ) {
               swap(v[i],v[j]);
               swap(v1[i],v1[j]);
               i++;
               j--;
               }
  }
  if (i<r) ssort(i,r);
  if (j>l) ssort(l,j);
}
int main()
{
   // freopen("input.txt", "r", stdin);
   //  freopen("output.txt", "w", stdout);
  cin>>s;
	for (int i=0;i<s.size();i++)
	{
		if (s[i]=='(')   b++;
		else
		if (s[i]==')')   b--;
		else {
		  cin>>l>>r;
		      b--;
		      s[i]=')';
		      c+=r;
		     v.push_back(l-r);
		     v1.push_back(i);
		     siz++;
		}
    }
        ssort(0,siz);
		//for (int i=0;i<=siz;i++)
         // cout<<v[i]<<" "<<v1[i]<<endl;
		while (b<0 && siz>0)
		{
		b+=2;
		s[v1[siz-1]]='(';
		c+=v[siz-1];
		siz--;
		if (b>0) break;
		if (siz==0) break;
	}
	if (b!=0) {cout<<-1<<endl;
	           return 0;
	           }
	 cout<<c<<endl;
	 cout<<s<<endl;

}


#include<bits/stdc++.h>
using namespace std;
string s;
vector <int> v ,v1;
long long b,c,l,r,siz=-1;

void ssort(int l, int r)
{
  int i=l,j=r,x=v[(i+j)/2];
  while (i<=j)
  {
    while (v[i]>x) i++;
    while (v[j]<x) j--;
    if (i<=j ) {
               swap(v[i],v[j]);
               swap(v1[i],v1[j]);
               i++;
               j--;
               }
  }
  if (i<r) ssort(i,r);
  if (j>l) ssort(l,j);
}
int main()
{
    //freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
  cin>>s;
	for (int i=0;i<s.size();i++)
	{
		if (s[i]=='(')   b++;
		else
		if (s[i]==')')   b--;
		else {
		  cin>>l>>r;
		      b--;
		      s[i]=')';
		      c+=r;
		     v.push_back(l-r);
		     v1.push_back(i);
		     siz++;
		}
    }
        ssort(0,siz);
		//for (int i=0;i<=siz;i++)
         //   cout<<v[i]<<" "<<v1[i]<<endl;

		while (b<0 && siz>0)
		{
		b+=2;
		s[v1[siz]]='(';
		c+=v[siz];
		siz--;
		if (b>=0) break;
		if (siz==0) break;
	}
	if (b!=0) {cout<<-1<<endl;
	           return 0;
	        }
	 cout<<c<<endl;
	 cout<<s<<endl;

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>

using namespace std;
using ll = long long;
using pii = pair<ll, int>;

constexpr int MAXN = 50004;
char s[MAXN];

int main() {
    scanf("%s", s);
    int n = strlen(s);
    ll score = 0;

    int prefix = 0;
    bool poss = true;

    int a, b;
    priority_queue<pii, vector<pii>, greater<pii> > pq;
    for (int i = 0; i < n; ++i) {
        if (s[i] == '(') {
            ++prefix;
        } else if (s[i] == ')') {
            --prefix;
        } else {
            scanf("%d %d", &a, &b);
            s[i] = ')';
            score += b;
            pq.push({a - b, i});
            // printf("Pushed %d %d\n", a - b, i);
            --prefix;
        }

        if (prefix < 0 and !pq.empty()) {
            pii cur = pq.top();
            pq.pop();

            // printf("Popped %d %d\n", cur.first, cur.second);
            score += cur.first;
            prefix += 2;
            s[cur.second] = '(';
        }

        if (prefix < 0) {
            poss = false;
            break;
        }
    }

    if (!poss or prefix != 0)
        return printf("%d\n", -1), 0;

    printf("%I64d\n", score);
    // printf("%lld\n", score);
    printf("%s\n", s);

    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>

using namespace std;
using ll = long long;
using pii = pair<ll, int>;

constexpr int MAXN = 50004;
char s[MAXN];

int main() {
    scanf("%s", s);
    int n = strlen(s);
    ll score = 0;

    int prefix = 0;
    bool poss = true;

    int a, b;
    priority_queue<pii, vector<pii>, greater<pii> > pq;
    for (int i = 0; i < n; ++i) {
        if (s[i] == '(') {
            ++prefix;
        } else if (s[i] == ')') {
            --prefix;
        } else {
            scanf("%d %d", &a, &b);
            s[i] = ')';
            score += b;
            pq.push({a - b, i});
            --prefix;
        }

        while (prefix < 0 and !pq.empty()) {
            pii cur = pq.top();
            pq.pop();
            score += cur.first;
            prefix += 2;
            s[i] = '(';
        }

        if (prefix < 0) {
            poss = false;
            break;
        }
    }

    if (!poss)
        return printf("%d\n", -1), 0;

    printf("%I64d\n", score);
    // printf("%lld\n", score);
    printf("%s\n", s);

    return 0;
}


import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.io.*;
import java.math.*;
public class Main12{
	static public void main(String args[])throws IOException{
		int tt=1;
		StringBuilder sb=new StringBuilder();
		for(int ttt=1;ttt<=tt;ttt++){
            int n=i();
            int m=i();
			long b=i();
			int size=(int)pow(2,m);
			long[] dp=new long[size];
			Arrays.fill(dp,(long)(1L<<60));
			ArrayList<Integer> ar=new ArrayList<>();
			long[] x=new long[n];
			long[] l=new long[n];
			int[] m1=new int[n];
			int[] value=new int[n];
			Pair[] pair=new Pair[n];
            for(int i=0;i<n;i++){
                x[i]=i();
                l[i]=i();
				m1[i]=i();
				int val=0;
				ArrayList<Integer> ar1=new ArrayList<>();
                for(int j=0;j<m1[i];j++){
					ar1.add(i()-1);
					val=val+(int)(pow(2,ar1.get(j)));
				}
				value[i]=val;
				pair[i]=new Pair(x[i],l[i],value[i]);
			}
			Arrays.sort(pair,new Comparator<Pair>(){
				@Override
				public int compare(Pair p1, Pair p2){
					if(p1.y<p2.y){
						return -1;
					}else if(p1.y==p2.y){
						return 0;
					}
					return 1;
				}
			});
			dp[0]=0;
			long ans=(1L>>60);
			for(int i=0;i<n;i++){
				for(int j=0;j<size;j++){
					dp[(j | pair[i].z)] = Math.min((dp[(j|pair[i].z)]),dp[j]+pair[i].x);
				}
				ans=Math.min(ans,dp[size-1]+pair[i].y*b);
			}
			if(ans>=(1L>>60)){
				sb.append("-1\n");
			}else{
				sb.append(ans+"\n");
			}
		}
		System.out.print(sb.toString());
	}
	static InputReader in=new InputReader(System.in);
	static OutputWriter out=new OutputWriter(System.out);
	static ArrayList<ArrayList<Integer>> graph;
	static int mod=1000000007;
	static class Pair{
		long x;
		long y;
		int z;
		Pair(long x,long y,int z){
			this.x=x;
			this.y=y;
			this.z=z;
		}
		/*@Override
		public int hashCode()
		{
			final int temp = 14;
			int ans = 1;
			ans =x*31+y*13;
			return (int)ans;
		}
       @Override
        public boolean equals(Object o)
        {
            if (this == o) {
                return true;
            }
            if (o == null) {
                return false;
            }
            if (this.getClass() != o.getClass()) {
                return false;
            }
            Pair other = (Pair)o;
            if (this.x != other.x || this.y!=other.y) {
                return false;
            }
            return true;
        }*/
	}
	public static int[] sort(int[] a){
		int n=a.length;
		ArrayList<Integer> ar=new ArrayList<>();
		for(int i=0;i<a.length;i++){
			ar.add(a[i]);
		}
		Collections.sort(ar);
		for(int i=0;i<n;i++){
			a[i]=ar.get(i);
		}
		return a;
	}
	public static long pow(long a, long b){
        long result=1;
        while(b>0){
            if (b % 2 != 0){
                result=(result*a);
                b--;
            }
            a=(a*a);
            b /= 2;
        }
        return result;
    }
	public static long gcd(long a, long b){
        if (a == 0){
            return b;
		}
        return gcd(b%a, a);
    }
	public static long lcm(long a, long b){
		return a*(b/gcd(a,b));
	}
	public static long l(){
		String s=in.String();
		return Long.parseLong(s);
	}
	public static void pln(String value){
		System.out.println(value);
	}
	public static int i(){
		return in.Int();
	}
	public static String s(){
		return in.String();
	}
}
class InputReader
{
	private InputStream stream;
	private byte[] buf = new byte[1024];
	private int curChar;
	private int numChars;
	private SpaceCharFilter filter;
	public InputReader(InputStream stream) {
		this.stream = stream;
	}
	public int read() {
		if (numChars== -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}
	public int Int() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}
	public String String() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}
	public boolean isSpaceChar(int c) {
		if (filter != null)
			return filter.isSpaceChar(c);
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}
	public String next() {
		return String();
	}
	public interface SpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}
class OutputWriter {
	private final PrintWriter writer;
	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
	}
	public OutputWriter(Writer writer) {
		this.writer = new PrintWriter(writer);
	}
	public void print(Object...objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}
	public void printLine(Object...objects) {
		print(objects);
		writer.println();
	}
	public void close() {
		writer.close();
	}
	public void flush() {
		writer.flush();
	}
 }
class IOUtils {
	public static int[] readIntArray(InputReader in, int size) {
		int[] array = new int[size];
		for (int i = 0; i < size; i++)
			array[i] = in.Int();
		return array;
	}
}


import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.io.*;
import java.math.*;
public class Main12{
	static public void main(String args[])throws IOException{
		int tt=1;
		StringBuilder sb=new StringBuilder();
		for(int ttt=1;ttt<=tt;ttt++){
            int n=i();
            int m=i();
			int b=i();
			int size=(int)pow(2,m);
			long[] dp=new long[size];
			long[] extra=new long[size];
			Arrays.fill(dp,Integer.MAX_VALUE);
			Arrays.fill(extra,Integer.MAX_VALUE);
			ArrayList<ArrayList<Integer>> ar=new ArrayList<>();
			int[] x=new int[n];
			int[] l=new int[n];
			int[] m1=new int[n];
            for(int i=0;i<n;i++){
                x[i]=i();
                l[i]=i();
				m1[i]=i();
				ArrayList<Integer> ar1=new ArrayList<>();
                for(int j=0;j<m1[i];j++){
                    ar1.add(i()-1);
				}
				ar.add(ar1);
				int siz=(int)pow(2,m1[i]);
				for(int j=0;j<siz;j++){
					ArrayList<Integer> ar2=new ArrayList<>();
					for(int k=0;k<ar1.size();k++){
						int value = (j & (int)(1<<k));
						if(value!=0){
							ar2.add(k);
						}
					}
					int val=0;
					for(int k=0;k<ar2.size();k++){
						val=val+(int)pow(2,ar1.get(ar2.get(k)));
					}
					if(dp[val]==Integer.MAX_VALUE){
						dp[val]=x[i];
						extra[val]=l[i];
					}else{
						if(dp[val]+extra[val]*b>x[i]+l[i]*b){
							dp[val]=x[i];
							extra[val]=l[i];
						}
					}
				}
			}
			dp[0]=0;
			extra[0]=0;
			//pln(Arrays.toString(dp));
			//pln(Arrays.toString(extra));
			for(int i=1;i<size;i++){
				int num=i;
				for(int j=0;j<m;j++){
					int value = num & ( 1 << j);
					if(value!=0){
						int tmp= num- ( 1<< j);
						long dpValue=dp[tmp];
						long Extra=extra[tmp];
						long value1=dpValue;
						long value2=dp[(1<<j)];
						if(dp[i]+extra[i]*b>value1+value2+b*Math.max(Extra,extra[(1<<j)])){
							dp[i]=value1+value2;
							extra[i]=Math.max(Extra,extra[(1<<j)]);
						}
					}
				}
			}
			//pln(Arrays.toString(dp));
			if(dp[size-1]==Integer.MAX_VALUE){
				sb.append("-1\n");
			}else{
				sb.append((dp[size-1]+extra[size-1]*b)+"\n");
			}
		}
		System.out.print(sb.toString());
	}
	static InputReader in=new InputReader(System.in);
	static OutputWriter out=new OutputWriter(System.out);
	static ArrayList<ArrayList<Integer>> graph;
	static int mod=1000000007;
	static class Pair{
		int x;
		int y;
		int z;
		Pair(int x,int y,int z){
			this.x=x;
			this.y=y;
			this.z=z;
		}
		/*@Override
		public int hashCode()
		{
			final int temp = 14;
			int ans = 1;
			ans =x*31+y*13;
			return (int)ans;
		}
       @Override
        public boolean equals(Object o)
        {
            if (this == o) {
                return true;
            }
            if (o == null) {
                return false;
            }
            if (this.getClass() != o.getClass()) {
                return false;
            }
            Pair other = (Pair)o;
            if (this.x != other.x || this.y!=other.y) {
                return false;
            }
            return true;
        }*/
	}
	public static int[] sort(int[] a){
		int n=a.length;
		ArrayList<Integer> ar=new ArrayList<>();
		for(int i=0;i<a.length;i++){
			ar.add(a[i]);
		}
		Collections.sort(ar);
		for(int i=0;i<n;i++){
			a[i]=ar.get(i);
		}
		return a;
	}
	public static long pow(long a, long b){
        long result=1;
        while(b>0){
            if (b % 2 != 0){
                result=(result*a);
                b--;
            }
            a=(a*a);
            b /= 2;
        }
        return result;
    }
	public static long gcd(long a, long b){
        if (a == 0){
            return b;
		}
        return gcd(b%a, a);
    }
	public static long lcm(long a, long b){
		return a*(b/gcd(a,b));
	}
	public static long l(){
		String s=in.String();
		return Long.parseLong(s);
	}
	public static void pln(String value){
		System.out.println(value);
	}
	public static int i(){
		return in.Int();
	}
	public static String s(){
		return in.String();
	}
}
class InputReader
{
	private InputStream stream;
	private byte[] buf = new byte[1024];
	private int curChar;
	private int numChars;
	private SpaceCharFilter filter;
	public InputReader(InputStream stream) {
		this.stream = stream;
	}
	public int read() {
		if (numChars== -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}
	public int Int() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}
	public String String() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}
	public boolean isSpaceChar(int c) {
		if (filter != null)
			return filter.isSpaceChar(c);
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}
	public String next() {
		return String();
	}
	public interface SpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}
class OutputWriter {
	private final PrintWriter writer;
	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
	}
	public OutputWriter(Writer writer) {
		this.writer = new PrintWriter(writer);
	}
	public void print(Object...objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}
	public void printLine(Object...objects) {
		print(objects);
		writer.println();
	}
	public void close() {
		writer.close();
	}
	public void flush() {
		writer.flush();
	}
 }
class IOUtils {
	public static int[] readIntArray(InputReader in, int size) {
		int[] array = new int[size];
		for (int i = 0; i < size; i++)
			array[i] = in.Int();
		return array;
	}
}


import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.io.*;
import java.math.*;
public class Main12{
	static public void main(String args[])throws IOException{
		int tt=1;
		StringBuilder sb=new StringBuilder();
		for(int ttt=1;ttt<=tt;ttt++){
            int n=i();
            int m=i();
			long b=i();
			int size=(int)pow(2,m);
			long[] dp=new long[size];
			long[] extra=new long[size];
			Arrays.fill(dp,Integer.MAX_VALUE);
			Arrays.fill(extra,Integer.MAX_VALUE);
			ArrayList<ArrayList<Integer>> ar=new ArrayList<>();
			int[] x=new int[n];
			int[] l=new int[n];
			int[] m1=new int[n];
            for(int i=0;i<n;i++){
                x[i]=i();
                l[i]=i();
				m1[i]=i();
				ArrayList<Integer> ar1=new ArrayList<>();
                for(int j=0;j<m1[i];j++){
                    ar1.add(i()-1);
				}
				ar.add(ar1);
				int siz=(int)pow(2,m1[i]);
				for(int j=0;j<siz;j++){
					ArrayList<Integer> ar2=new ArrayList<>();
					for(int k=0;k<ar1.size();k++){
						int value = (j & (int)(1<<k));
						if(value!=0){
							ar2.add(k);
						}
					}
					int val=0;
					for(int k=0;k<ar2.size();k++){
						val=val+(int)pow(2,ar1.get(ar2.get(k)));
					}
					if(val>0){
					if(dp[val]==Integer.MAX_VALUE){
						dp[val]=x[i];
						extra[val]=l[i];
					}else{
					//	pln(dp[val]+" "+extra[val]+" "+x[i]+" "+l[i]);
					//	pln((dp[val]+extra[val]*b)+" "+(x[i]+l[i]*b));
						if(dp[val]+extra[val]*b>x[i]+l[i]*b){
							dp[val]=x[i];
							extra[val]=l[i];
						}
					}}
				}
				//pln(dp[1]+" "+extra[1]);
			}
			dp[0]=0;
			extra[0]=0;
			//pln(Arrays.toString(dp));
			//pln(Arrays.toString(extra));
			for(int i=1;i<size;i++){
				int num=i;
				for(int j=0;j<m;j++){
					int value = num & ( 1 << j);
					if(value!=0){
						int tmp= num- ( 1<< j);
						long dpValue=dp[tmp];
						long Extra=extra[tmp];
						long value1=dpValue;
						long value2=dp[(1<<j)];
						if(dp[i]+extra[i]*b>value1+value2+b*Math.max(Extra,extra[(1<<j)])){
							dp[i]=value1+value2;
							extra[i]=Math.max(Extra,extra[(1<<j)]);
						}
					}
				}
			}
			//pln(Arrays.toString(dp));
			if(dp[size-1]==Integer.MAX_VALUE){
				sb.append("-1\n");
			}else{
				sb.append((dp[size-1]+extra[size-1]*b)+"\n");
			}
		}
		System.out.print(sb.toString());
	}
	static InputReader in=new InputReader(System.in);
	static OutputWriter out=new OutputWriter(System.out);
	static ArrayList<ArrayList<Integer>> graph;
	static int mod=1000000007;
	static class Pair{
		int x;
		int y;
		int z;
		Pair(int x,int y,int z){
			this.x=x;
			this.y=y;
			this.z=z;
		}
		/*@Override
		public int hashCode()
		{
			final int temp = 14;
			int ans = 1;
			ans =x*31+y*13;
			return (int)ans;
		}
       @Override
        public boolean equals(Object o)
        {
            if (this == o) {
                return true;
            }
            if (o == null) {
                return false;
            }
            if (this.getClass() != o.getClass()) {
                return false;
            }
            Pair other = (Pair)o;
            if (this.x != other.x || this.y!=other.y) {
                return false;
            }
            return true;
        }*/
	}
	public static int[] sort(int[] a){
		int n=a.length;
		ArrayList<Integer> ar=new ArrayList<>();
		for(int i=0;i<a.length;i++){
			ar.add(a[i]);
		}
		Collections.sort(ar);
		for(int i=0;i<n;i++){
			a[i]=ar.get(i);
		}
		return a;
	}
	public static long pow(long a, long b){
        long result=1;
        while(b>0){
            if (b % 2 != 0){
                result=(result*a);
                b--;
            }
            a=(a*a);
            b /= 2;
        }
        return result;
    }
	public static long gcd(long a, long b){
        if (a == 0){
            return b;
		}
        return gcd(b%a, a);
    }
	public static long lcm(long a, long b){
		return a*(b/gcd(a,b));
	}
	public static long l(){
		String s=in.String();
		return Long.parseLong(s);
	}
	public static void pln(String value){
		System.out.println(value);
	}
	public static int i(){
		return in.Int();
	}
	public static String s(){
		return in.String();
	}
}
class InputReader
{
	private InputStream stream;
	private byte[] buf = new byte[1024];
	private int curChar;
	private int numChars;
	private SpaceCharFilter filter;
	public InputReader(InputStream stream) {
		this.stream = stream;
	}
	public int read() {
		if (numChars== -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}
	public int Int() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}
	public String String() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}
	public boolean isSpaceChar(int c) {
		if (filter != null)
			return filter.isSpaceChar(c);
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}
	public String next() {
		return String();
	}
	public interface SpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}
class OutputWriter {
	private final PrintWriter writer;
	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
	}
	public OutputWriter(Writer writer) {
		this.writer = new PrintWriter(writer);
	}
	public void print(Object...objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}
	public void printLine(Object...objects) {
		print(objects);
		writer.println();
	}
	public void close() {
		writer.close();
	}
	public void flush() {
		writer.flush();
	}
 }
class IOUtils {
	public static int[] readIntArray(InputReader in, int size) {
		int[] array = new int[size];
		for (int i = 0; i < size; i++)
			array[i] = in.Int();
		return array;
	}
}


import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.io.*;
import java.math.*;
public class Main12{
	static public void main(String args[])throws IOException{
		int tt=1;
		StringBuilder sb=new StringBuilder();
		for(int ttt=1;ttt<=tt;ttt++){
            int n=i();
            int m=i();
			int b=i();
			int size=(int)pow(2,m);
			long[] dp=new long[size];
			long[] extra=new long[size];
			Arrays.fill(dp,Integer.MAX_VALUE);
			Arrays.fill(extra,Integer.MAX_VALUE);
			ArrayList<ArrayList<Integer>> ar=new ArrayList<>();
			int[] x=new int[n];
			int[] l=new int[n];
			int[] m1=new int[n];
            for(int i=0;i<n;i++){
                x[i]=i();
                l[i]=i();
				m1[i]=i();
				ArrayList<Integer> ar1=new ArrayList<>();
                for(int j=0;j<m1[i];j++){
                    ar1.add(i()-1);
				}
				ar.add(ar1);
				int siz=(int)pow(2,m1[i]);
				for(int j=0;j<siz;j++){
					ArrayList<Integer> ar2=new ArrayList<>();
					for(int k=0;k<ar1.size();k++){
						int value = (j & (int)(1<<k));
						if(value!=0){
							ar2.add(k);
						}
					}
					int val=0;
					for(int k=0;k<ar2.size();k++){
						val=val+(int)pow(2,ar1.get(ar2.get(k)));
					}
					if(dp[val]==Integer.MAX_VALUE){
						dp[val]=x[i];
						extra[val]=l[i];
					}else{
						if(dp[val]+extra[val]>x[i]+l[i]){
							dp[val]=x[i];
							extra[val]=l[i];
						}
					}
				}
			}
			dp[0]=0;
			extra[0]=0;
			//pln(Arrays.toString(dp));
			//pln(Arrays.toString(extra));
			for(int i=1;i<size;i++){
				int num=i;
				for(int j=0;j<m;j++){
					int value = num & ( 1 << j);
					if(value!=0){
						int tmp= num- ( 1<< j);
						long dpValue=dp[tmp];
						long Extra=extra[tmp];
						long value1=dpValue;
						long value2=dp[(1<<j)];
						if(dp[i]+extra[i]*b>value1+value2+b*Math.max(Extra,extra[(1<<j)])){
							dp[i]=value1+value2;
							extra[i]=Math.max(Extra,extra[(1<<j)]);
						}
					}
				}
			}
			//pln(Arrays.toString(dp));
			if(dp[size-1]==Integer.MAX_VALUE){
				sb.append("-1\n");
			}else{
				sb.append((dp[size-1]+extra[size-1]*b)+"\n");
			}
		}
		System.out.print(sb.toString());
	}
	static InputReader in=new InputReader(System.in);
	static OutputWriter out=new OutputWriter(System.out);
	static ArrayList<ArrayList<Integer>> graph;
	static int mod=1000000007;
	static class Pair{
		int x;
		int y;
		int z;
		Pair(int x,int y,int z){
			this.x=x;
			this.y=y;
			this.z=z;
		}
		/*@Override
		public int hashCode()
		{
			final int temp = 14;
			int ans = 1;
			ans =x*31+y*13;
			return (int)ans;
		}
       @Override
        public boolean equals(Object o)
        {
            if (this == o) {
                return true;
            }
            if (o == null) {
                return false;
            }
            if (this.getClass() != o.getClass()) {
                return false;
            }
            Pair other = (Pair)o;
            if (this.x != other.x || this.y!=other.y) {
                return false;
            }
            return true;
        }*/
	}
	public static int[] sort(int[] a){
		int n=a.length;
		ArrayList<Integer> ar=new ArrayList<>();
		for(int i=0;i<a.length;i++){
			ar.add(a[i]);
		}
		Collections.sort(ar);
		for(int i=0;i<n;i++){
			a[i]=ar.get(i);
		}
		return a;
	}
	public static long pow(long a, long b){
        long result=1;
        while(b>0){
            if (b % 2 != 0){
                result=(result*a);
                b--;
            }
            a=(a*a);
            b /= 2;
        }
        return result;
    }
	public static long gcd(long a, long b){
        if (a == 0){
            return b;
		}
        return gcd(b%a, a);
    }
	public static long lcm(long a, long b){
		return a*(b/gcd(a,b));
	}
	public static long l(){
		String s=in.String();
		return Long.parseLong(s);
	}
	public static void pln(String value){
		System.out.println(value);
	}
	public static int i(){
		return in.Int();
	}
	public static String s(){
		return in.String();
	}
}
class InputReader
{
	private InputStream stream;
	private byte[] buf = new byte[1024];
	private int curChar;
	private int numChars;
	private SpaceCharFilter filter;
	public InputReader(InputStream stream) {
		this.stream = stream;
	}
	public int read() {
		if (numChars== -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}
	public int Int() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}
	public String String() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}
	public boolean isSpaceChar(int c) {
		if (filter != null)
			return filter.isSpaceChar(c);
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}
	public String next() {
		return String();
	}
	public interface SpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}
class OutputWriter {
	private final PrintWriter writer;
	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
	}
	public OutputWriter(Writer writer) {
		this.writer = new PrintWriter(writer);
	}
	public void print(Object...objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}
	public void printLine(Object...objects) {
		print(objects);
		writer.println();
	}
	public void close() {
		writer.close();
	}
	public void flush() {
		writer.flush();
	}
 }
class IOUtils {
	public static int[] readIntArray(InputReader in, int size) {
		int[] array = new int[size];
		for (int i = 0; i < size; i++)
			array[i] = in.Int();
		return array;
	}
}


import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.io.*;
import java.math.*;
public class Main{
	static public void main(String args[])throws IOException{
		int tt=1;
		StringBuilder sb=new StringBuilder();
		for(int ttt=1;ttt<=tt;ttt++){
            int n=i();
            int m=i();
			long b=i();
			int size=(int)pow(2,m);
			long[] dp=new long[size];
			Arrays.fill(dp,Long.MAX_VALUE);
			ArrayList<Integer> ar=new ArrayList<>();
			long[] x=new long[n];
			long[] l=new long[n];
			int[] m1=new int[n];
			int[] value=new int[n];
			Pair[] pair=new Pair[n];
            for(int i=0;i<n;i++){
                x[i]=i();
                l[i]=i();
				m1[i]=i();
				int val=0;
				ArrayList<Integer> ar1=new ArrayList<>();
                for(int j=0;j<m1[i];j++){
					ar1.add(i()-1);
					val=val+(int)(pow(2,ar1.get(j)));
				}
				value[i]=val;
				pair[i]=new Pair(x[i],l[i],value[i]);
			}
			Arrays.sort(pair,new Comparator<Pair>(){
				@Override
				public int compare(Pair p1, Pair p2){
					if(p1.y<p2.y){
						return -1;
					}else if(p1.y==p2.y){
						return 0;
					}
					return 1;
				}
			});
			dp[0]=0;
			long ans=Long.MAX_VALUE;
			for(int i=0;i<n;i++){
				for(int j=0;j<size;j++){
					if(dp[j]!=Long.MAX_VALUE){
					dp[(j | pair[i].z)] = Math.min((dp[(j|pair[i].z)]),dp[j]+pair[i].x);}
				}
				if(dp[size-1]!=Long.MAX_VALUE)
				ans=Math.min(ans,dp[size-1]+pair[i].y*b);
			}
			if(ans==Long.MAX_VALUE){
				sb.append("-1\n");
			}else{
				sb.append(ans+"\n");
			}
		}
		System.out.print(sb.toString());
	}
	static InputReader in=new InputReader(System.in);
	static OutputWriter out=new OutputWriter(System.out);
	static ArrayList<ArrayList<Integer>> graph;
	static int mod=1000000007;
	static class Pair{
		long x;
		long y;
		int z;
		Pair(long x,long y,int z){
			this.x=x;
			this.y=y;
			this.z=z;
		}
		/*@Override
		public int hashCode()
		{
			final int temp = 14;
			int ans = 1;
			ans =x*31+y*13;
			return (int)ans;
		}
       @Override
        public boolean equals(Object o)
        {
            if (this == o) {
                return true;
            }
            if (o == null) {
                return false;
            }
            if (this.getClass() != o.getClass()) {
                return false;
            }
            Pair other = (Pair)o;
            if (this.x != other.x || this.y!=other.y) {
                return false;
            }
            return true;
        }*/
	}
	public static int[] sort(int[] a){
		int n=a.length;
		ArrayList<Integer> ar=new ArrayList<>();
		for(int i=0;i<a.length;i++){
			ar.add(a[i]);
		}
		Collections.sort(ar);
		for(int i=0;i<n;i++){
			a[i]=ar.get(i);
		}
		return a;
	}
	public static long pow(long a, long b){
        long result=1;
        while(b>0){
            if (b % 2 != 0){
                result=(result*a);
                b--;
            }
            a=(a*a);
            b /= 2;
        }
        return result;
    }
	public static long gcd(long a, long b){
        if (a == 0){
            return b;
		}
        return gcd(b%a, a);
    }
	public static long lcm(long a, long b){
		return a*(b/gcd(a,b));
	}
	public static long l(){
		String s=in.String();
		return Long.parseLong(s);
	}
	public static void pln(String value){
		System.out.println(value);
	}
	public static int i(){
		return in.Int();
	}
	public static String s(){
		return in.String();
	}
}
class InputReader
{
	private InputStream stream;
	private byte[] buf = new byte[1024];
	private int curChar;
	private int numChars;
	private SpaceCharFilter filter;
	public InputReader(InputStream stream) {
		this.stream = stream;
	}
	public int read() {
		if (numChars== -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}
	public int Int() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}
	public String String() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}
	public boolean isSpaceChar(int c) {
		if (filter != null)
			return filter.isSpaceChar(c);
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}
	public String next() {
		return String();
	}
	public interface SpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}
class OutputWriter {
	private final PrintWriter writer;
	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
	}
	public OutputWriter(Writer writer) {
		this.writer = new PrintWriter(writer);
	}
	public void print(Object...objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}
	public void printLine(Object...objects) {
		print(objects);
		writer.println();
	}
	public void close() {
		writer.close();
	}
	public void flush() {
		writer.flush();
	}
 }
class IOUtils {
	public static int[] readIntArray(InputReader in, int size) {
		int[] array = new int[size];
		for (int i = 0; i < size; i++)
			array[i] = in.Int();
		return array;
	}
}


import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.io.*;
import java.math.*;
public class Main12{
	static public void main(String args[])throws IOException{
		int tt=1;
		StringBuilder sb=new StringBuilder();
		for(int ttt=1;ttt<=tt;ttt++){
            int n=i();
            int m=i();
			long b=i();
			int size=(int)pow(2,m);
			long[] dp=new long[size];
			Arrays.fill(dp,(long)(1L<<60));
			ArrayList<Integer> ar=new ArrayList<>();
			long[] x=new long[n];
			long[] l=new long[n];
			int[] m1=new int[n];
			int[] value=new int[n];
			Pair[] pair=new Pair[n];
            for(int i=0;i<n;i++){
                x[i]=i();
                l[i]=i();
				m1[i]=i();
				int val=0;
				ArrayList<Integer> ar1=new ArrayList<>();
                for(int j=0;j<m1[i];j++){
					ar1.add(i()-1);
					val=val+(int)(pow(2,ar1.get(j)));
				}
				value[i]=val;
				pair[i]=new Pair(x[i],l[i],value[i]);
			}
			Arrays.sort(pair,new Comparator<Pair>(){
				@Override
				public int compare(Pair p1, Pair p2){
					if(p1.y<p2.y){
						return -1;
					}else if(p1.y==p2.y){
						return 0;
					}
					return 1;
				}
			});
			dp[0]=0;
			long ans=Long.MAX_VALUE;
			for(int i=0;i<n;i++){
				for(int j=0;j<size;j++){
					dp[(j | pair[i].z)] = Math.min((dp[(j|pair[i].z)]),dp[j]+pair[i].x);
				}
				ans=Math.min(ans,dp[size-1]+pair[i].y*b);
			}
			if(ans==Long.MAX_VALUE){
				sb.append("-1\n");
			}else{
				sb.append(ans+"\n");
			}
		}
		System.out.print(sb.toString());
	}
	static InputReader in=new InputReader(System.in);
	static OutputWriter out=new OutputWriter(System.out);
	static ArrayList<ArrayList<Integer>> graph;
	static int mod=1000000007;
	static class Pair{
		long x;
		long y;
		int z;
		Pair(long x,long y,int z){
			this.x=x;
			this.y=y;
			this.z=z;
		}
		/*@Override
		public int hashCode()
		{
			final int temp = 14;
			int ans = 1;
			ans =x*31+y*13;
			return (int)ans;
		}
       @Override
        public boolean equals(Object o)
        {
            if (this == o) {
                return true;
            }
            if (o == null) {
                return false;
            }
            if (this.getClass() != o.getClass()) {
                return false;
            }
            Pair other = (Pair)o;
            if (this.x != other.x || this.y!=other.y) {
                return false;
            }
            return true;
        }*/
	}
	public static int[] sort(int[] a){
		int n=a.length;
		ArrayList<Integer> ar=new ArrayList<>();
		for(int i=0;i<a.length;i++){
			ar.add(a[i]);
		}
		Collections.sort(ar);
		for(int i=0;i<n;i++){
			a[i]=ar.get(i);
		}
		return a;
	}
	public static long pow(long a, long b){
        long result=1;
        while(b>0){
            if (b % 2 != 0){
                result=(result*a);
                b--;
            }
            a=(a*a);
            b /= 2;
        }
        return result;
    }
	public static long gcd(long a, long b){
        if (a == 0){
            return b;
		}
        return gcd(b%a, a);
    }
	public static long lcm(long a, long b){
		return a*(b/gcd(a,b));
	}
	public static long l(){
		String s=in.String();
		return Long.parseLong(s);
	}
	public static void pln(String value){
		System.out.println(value);
	}
	public static int i(){
		return in.Int();
	}
	public static String s(){
		return in.String();
	}
}
class InputReader
{
	private InputStream stream;
	private byte[] buf = new byte[1024];
	private int curChar;
	private int numChars;
	private SpaceCharFilter filter;
	public InputReader(InputStream stream) {
		this.stream = stream;
	}
	public int read() {
		if (numChars== -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}
	public int Int() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}
	public String String() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}
	public boolean isSpaceChar(int c) {
		if (filter != null)
			return filter.isSpaceChar(c);
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}
	public String next() {
		return String();
	}
	public interface SpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}
class OutputWriter {
	private final PrintWriter writer;
	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
	}
	public OutputWriter(Writer writer) {
		this.writer = new PrintWriter(writer);
	}
	public void print(Object...objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}
	public void printLine(Object...objects) {
		print(objects);
		writer.println();
	}
	public void close() {
		writer.close();
	}
	public void flush() {
		writer.flush();
	}
 }
class IOUtils {
	public static int[] readIntArray(InputReader in, int size) {
		int[] array = new int[size];
		for (int i = 0; i < size; i++)
			array[i] = in.Int();
		return array;
	}
}