#include <iostream>
#include <vector>
#include <string>
/*
Note: In the output description of the problem, write as follows:
"Output the number of correct sentences that can be made by switching the direction of one parenthesis (without changing any other parenthesis)"
-> Because there is only one sample for input/ouput.
So, to be honest, I don't know exactly what the desired output of the program is in the following cases because it affects the direction of solving the problem
1. If the input string is can't be fixed, eg.: "(()"
-> Case 1: Program need to return 0 because it is incorrect sentences
-> Case 2: Return number of correct sentences that can be made (in this case: return 1)
2. If the input string is able to fix, but it need to be fixed at 2 or more positions, eg.: "(()(((()"
-> I need to fix at 1 point then return output as described in <case 2> OR return 0 because it's still considered invalid ?
-> So I will design as:
1. Check if the string can be fixed by switching the direction of one parenthesis, return the number of correct sentences that can be made after fix
2. If the input string is correct, return the number of correct sentences that can be made
3. If the input string is can't be fixed or string is able to fix, but it need to be fixed at at 2 or more positions -> return 0
*/
using namespace std;
bool IsNeedToFixed(const vector<int>& balance, int pos, bool isOpen)
{
if (!isOpen)
return balance[pos + 1] == -1;
int tmp = 0;
for (int j = pos + 1; j < balance.size(); ++j)
{
tmp = balance[j] - 2;
if (tmp < 0)
return false;
}
return (tmp == 0);
}
int CountCorrect(const string& s)
{
int n = s.length();
if (n % 2 != 0)
return 0;
vector<int> balance(n + 1, 0);
for (int i = 0; i < n; ++i)
balance[i + 1] = balance[i] + (s[i] == '(' ? 1 : -1);
int totalBalance = balance[n];
if (abs(totalBalance) != 2)
{
if (totalBalance == 0)
return n / 2;
return 0;
}
string tmp = s;
bool isOpen = (totalBalance == 2) ? true : false;
char cmp = isOpen ? '(' : ')';
for (int i = 0; i < n; ++i)
{
if (s[i] != cmp)
continue;
if (IsNeedToFixed(balance, i, isOpen))
{
tmp[i] = (isOpen) ? ')' : '('; // change direction
break;
}
}
// recheck balace for confirm
for (int i = 0; i < n; ++i)
balance[i + 1] = balance[i] + (tmp[i] == '(' ? 1 : -1);
return (balance.back() == 0) ? n / 2 : 0;
}
int main() {
string s;
cin >> s;
cout << CountCorrect(s) << endl;
return 0;
}