The Monster
Contest: Codeforces Round 459 (Div. 1)
Difficulty rating: 1800
Tags: dp, greedy, implementation, math

As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him.

Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.

A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally:

- Empty string is a correct bracket sequence.
- if s is a correct bracket sequence, then (s) is also a correct bracket sequence.
- if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence.

A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.

Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≤ l ≤ r ≤ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.

Joyce doesn't know anything about bracket sequences, so she asked for your help.

Input format:
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≤ |s| ≤ 5000).

Output format:
Print the answer to Will's puzzle in the first and only line of output.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
For the first sample testcase, the pretty substrings of s are:

1. "(?" which can be transformed to "()".
2. "?)" which can be transformed to "()".
3. "((?)" which can be transformed to "(())".
4. "(?))" which can be transformed to "(())".

For the second sample testcase, the pretty substrings of s are:

1. "??" which can be transformed to "()".
2. "()".
3. "??()" which can be transformed to "()()".
4. "?()?" which can be transformed to "(())".
5. "??" which can be transformed to "()".
6. "()??" which can be transformed to "()()".
7. "??()??" which can be transformed to "()()()".

Example input:
((?))

Example output:
4

Example input:
??()??

Example output:
7

Editorial:
First, let's denote s[l..r] as the substring slsl + 1... sr of string s. Also s.count(t) is the number of occurrences of t in s.
A string consisting of parentheses and question marks is pretty if and only if:
1. |s| is even.
2. 0 ≤ s[1..i].count('(') + s[1..i].count('?') - s[1..i].count(')') for each 1 ≤ i ≤ |s|.
3. 0 ≤ s[i..|s|].count(')') + s[i..|s|].count('?') - s[i..|s|].count('(') for each 1 ≤ i ≤ |s|.
Proof: If s.count('?') = 0 then s is a correct bracket sequence. Otherwise, let q be an integer between 1 to |s| such that sq = '?'.
Lemma: We can replace sq by either '(' or ')' such that the three conditions above remain satisfied.
Proof: We'll use proof by contradiction. If we can replace sq by either '(' or ')' such that the conditions remain satisfied, the lemma is proven. Otherwise, the conditions will be violated if we replace sq by '(' or ')'.
Let's denote f(s) as s.count('(') + s.count('?') - s.count(')') and g(s) as s.count(')') + s.count('?') - s.count('('). Please note that f(s) =  - g(s) + 2 × s.count('?') and g(s) =  - f(s) + 2 × s.count('?').
By assumption, if we replace sq by '(' the conditions will be violated. By replacing sq the second condition can't be violated, thus the third condition will be violated. So, there's an integer i such that 1 ≤ i ≤ q and g(t[i..|t|]) < 0 (t is s after replacing sq by '('). Thus, g(s[i..|s|]) < 2. Similarly, there's an integer j such that q ≤ j ≤ |s| and f(s[1..j]) < 2.
Since all three conditions are satisfied for s (by assumption), then 0 ≤ g(s[i..|s|]), f(s[1..j]) ≤ 1.
Let's break s into three parts (they could be empty): a = s[1..(i - 1)], b = s[i..j] and c = s[(j + 1)..|s|].
g(s[i..|s|]) = g(b) + g(c) and f(s[1..j]) = f(a) + f(b). Since the three conditions are satisfied for s, then 0 ≤ g(c), f(a).
f(a) + f(b) ≤ 1 so f(a) - 1 ≤  - f(b). Thus f(a) - 1 ≤ g(b) - 2 × b.count('?'), so f(a) - 1 + 2 × b.count('?') ≤ g(b).
So f(a) - 1 + 2 × b.count('?') + g(c) ≤ g(b) + g(c) ≤ 1. So f(a) - 1 + 2 × b.count('?') + g(c) ≤ 1. Since i ≤ q ≤ j, then 2 ≤ 2 × b.count('?').
Also, 0 ≤ g(c), f(a). So, 1 ≤ f(a) - 1 + 2 × b.count('?') + g(c) ≤ 1. So f(a) - 1 + 2 × b.count('?') + g(c) = 1. This requires that f(a) = g(c) = 0 and b.count('?') = 1.
Since f(a) and g(c) are even, then |a| and |c| are even, and since |s| is even (first condition), then |b| is also even (because |s| = |a| + |b| + |c|).
f(a) = g(c) = 0 and 0 ≤ f(a) + f(b) and 0 ≤ g(b) + g(c), thus 0 ≤ f(b), g(b). Also, f(a) + f(b), g(b) + g(c) ≤ 1, thus 0 ≤ f(b), g(b) ≤ 1, since |b| is even, f(b) and g(b) are also even, thus, f(b) = g(b) = 0. g(b) =  - f(b) + 2 × b.count('?') and since 1 ≤ b.count('?') then g(b) ≠ 0.
Thus, we have 0 ≠ 0, which is false. So the lemma is true.
Using the lemma above, each time we can replace a question mark by parentheses and at the end we get a correct bracket sequence.
After proof: Knowing this fact, we can find all such substrings by checking the three conditions. Pseudo code:
Total time complexity: $${\mathcal {O}}(n^{2})$$ where n = |s|