402346: GYM100735 A Strong parentheses sequence

Memory Limit:64 MB Time Limit:0 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

A. Strong parentheses sequencetime limit per test0.25 secondsmemory limit per test64 megabytesinputstandard inputoutputstandard output

A sequence is called a correct parenthesis sequence if it's the following form:

- Empty sequence is considered a correct parenthesis sequence.

- (A) is considered a correct parenthesis sequence.

- XY is considered a correct parenthesis sequence, if both X and Y are correct parenthesis sequences.

A sequence is called a strong parenthesis sequence only if it on form (A), where A is a correct parenthesis sequence.

You have to calculate a weird sum: take [i1, j1] and [i2, j2] every two subarrays of A. The subarrays must not intersect (i.e. i1 ≤ i2 and j1 ≤ i2) and must be strong parenthesis sequences. Then, we add to the sum the minimum length of a subarray, such as it is also a strong parenthesis sequence and it contains both [i1, j1] and [i2, j2] (i.e. if a subsequence of minimum length is [i3, j3], then [i3, j3] is a strong parenthesis sequence and also i3 ≤ i1 ≤ j1 ≤ j3 and i3 ≤ i2 ≤ j2 ≤ j3).

Output the calculated sum.

Input

The input contains the sequence A – a strong parenthesis sequence. It's length is between 2 and 100000.

Output

The output contains a single line - the sum requested by the problem.

ExamplesInput
(()())
Output
6
Input
(()(()))
Output
16
Note

In the first test case the answer is length of "([][])".

In the second test case the answer is the sum of lenghts of "([][()])" and "([]([]))".

(The strong parentheses sequences [i1, j1] and [i2, j2] emphasized by "[...]").

加入题单

上一题 下一题 算法标签: