题意:给你一串序列,有左括号右括号和问号,现在需要你把问号变成括号,求最小代价把这个序列变成合法的,如果没有则输出-1
思路:直接暴力扫一遍,先将问号的都变成右括号,如果当前这个括号的位置<0的话,那么就把y-x最大的返回回去变成左括号就行了
#include<bits/stdc++.h>
using namespace std;
priority_queue<pair<long long,int> >Q;
string s;
int main()
{
cin>>s;
if(s.size()%2==1)return puts("-1"),0;
int now=0;
long long ans=0;
for(int i=0;i<s.size();i++)
{
if(s[i]=='(')
now++;
else if(s[i]==')')
now--;
else
{
int x,y;scanf("%d%d",&x,&y);
now--;s[i]=')';ans+=y;
Q.push(make_pair(y-x,i));
}
if(now<0)
{
if(Q.size()==0)break;
pair<int,int> a = Q.top();Q.pop();
ans-=a.first;s[a.second]='(';
now+=2;
}
}
if(now!=0)return puts("-1"),0;
cout<<ans<<endl;
cout<<s<<endl;
}