https://www.acwing.com/problem/content/submission/4210/
#include <iostream>
#include <stack>
using namespace std;
const int N = 1e6 + 10;
char str[N];
char sta[N];
int hh = 0;
int cnt;
int main()
{
cin >> str;
for (int i = 0; str[i]; i ++)
if(str[i] == '(') sta[++ hh] = '(';
else if(hh) cnt += 2, hh --;
printf("%d", cnt);
return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1000010;
char str[N];
int main()
{
scanf("%s", str);
int l = 0, r = 0;
for (int i = 0; str[i]; i ++ )
if (str[i] == '(') l ++ ;
else if (l > 0)
{
l -- ;
r ++ ;
}
printf("%d\n", r * 2);
return 0;
}