大意: 记$f(t)$表示字符串$t$的最长括号匹配子序列, 给定n个括号序列, 求它们重排后的最大f(t).
首先可以注意到一个括号序列中已经匹配的可以直接消去, 一定不会影响最优解.
那么这样最终就为n个类似于))))((的括号序列, 然后贪心排序即可
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <string.h>
#include <queue>
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std;
const int N = 5e6+10, INF = 0x3f3f3f3f;
char s[N];
struct _ {
int l, r;
bool operator < (const _& x) const {
if (l>r&&x.l<=x.r) return 1;
if (l<=r&&x.l>x.r) return 0;
if (l<=r&&x.l<=x.r) return l>x.l;
return r<x.r;
}
} a[N];
void work() {
int n;
scanf("%d", &n);
int ans = 0, ll = 0, rr = 0;
REP(i,1,n) {
scanf("%s", s+1);
int sum = 0, L = 0, R = 0, m = strlen(s+1);
REP(i,1,m) {
if (s[i]==')') sum--;
else sum++;
if (sum<0) L-=sum,sum=0;
}
R = sum;
ans += m-L-R;
a[i].l=L, a[i].r=R;
}
sort(a+1,a+1+n);
int L = 0;
REP(i,1,n) {
a[i].r = min(a[i].r,L);
ans += a[i].r*2;
L -= a[i].r;
L += a[i].l;
}
printf("%d\n", ans);
}
int main() {
int t;
scanf("%d", &t);
REP(i,1,t) work();
}