1 #define bug(x) cout<<#x<<" is "<<x<<endl
2 #define IO std::ios::sync_with_stdio(0)
3 #include <bits/stdc++.h>
4 using namespace std;
5 typedef long long ll;
6 #define mk make_pair
7 #define pb push_back
8 const int inf=2147483647;
9 const int N=1e5+10;
10 int n,m;
11
12 char s[N];
13
14 priority_queue<ll>q;
15
16 int main(){
17
18 scanf("%s",s+1);
19 n=strlen(s+1);
20 int k=0;
21 ll ans=0;
22 for(int i=1;i<=n;i++){
23 if(s[i]=='(')k++;
24 else if(s[i]==')'){
25 k--;
26 if(k<0){
27 if(q.empty()){
28 printf("-1\n");
29 return 0;
30 }
31 else{
32 ans-=q.top();
33 q.pop();
34 k+=2;
35 }
36 }
37 }
38 else{
39 ll x,y;
40 scanf("%lld%lld",&x,&y);
41 if(k>0){
42 k--;
43 ans+=y;
44 q.push(y-x);
45 }
46 else if(!q.empty()&&q.top()>y-x){
47 k++;
48 ans-=q.top();
49 q.pop();
50 ans+=y;
51 q.push(y-x);
52 }
53 else{
54 k++;
55 ans+=x;
56 }
57 }
58 }
59 if(k)ans=-1;
60 printf("%lld\n",ans);
61 }