Compressed Bracket Sequence solution codeforces

For Solution
Click Here!
For example for a bracket sequence “((())()))” a corresponding sequence of numbers is [3,2,1,3][3,2,1,3]. Compressed Bracket Sequence solution codeforces
You need to find the total number of continuous subsequences (subsegments) [l,r][l,r] (l≤rl≤r) of the original bracket sequence, which are regular bracket sequences.
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters “+” and “1” into this sequence. For example, sequences “(())()“, “()” and “(()(()))” are regular, while “)(“, “(()” and “(()))(” are not.
The first line contains a single integer nn (1≤n≤1000)(1≤n≤1000), the size of the compressed sequence.
The second line contains a sequence of integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤109)(1≤ci≤109), the compressed sequence.
Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences.
It is guaranteed that the answer fits in the 64bit integer data type.
5 4 1 2 3 1
5
6 1 3 2 1 2 4
6
6 1 1 1 1 2 2
7
In the first example a sequence (((()(()))( is described. This bracket sequence contains 55 subsegments which form regular bracket sequences:
 Subsequence from the 33rd to 1010th character: (()(()))
 Subsequence from the 44th to 55th character: ()
 Subsequence from the 44th to 99th character: ()(())
 Subsequence from the 66th to 99th character: (())
 Subsequence from the 77th to 88th character: ()

For Solution
Click Here!