IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2)
F
Paper task
Codeforces
2600
原题
← EG →
翻译中…

Alex was programming while Valentina (his toddler daughter) got there and started asking many questions about the round brackets (or parenthesis) in the code. He explained her a bit and when she got it he gave her a task in order to finish his code on time.

翻译中…

For the purpose of this problem we consider only strings consisting of opening and closing round brackets, that is characters '(' and ')'.

翻译中…

The sequence of brackets is called correct if:

翻译中…
  1. it's empty;
  2. it's a correct sequence of brackets, enclosed in a pair of opening and closing brackets;
  3. it's a concatenation of two correct sequences of brackets.
翻译中…

For example, the sequences "()()" and "((()))(())" are correct, while ")(()", "(((((" and "())" are not.

翻译中…

Alex took a piece of paper, wrote a string $*s*$ consisting of brackets and asked Valentina to count the number of distinct non-empty substrings of $*s*$ that are correct sequences of brackets. In other words, her task is to count the number of non-empty correct sequences of brackets that occur in a string $*s*$ as a substring (don't mix up with subsequences).

翻译中…

When Valentina finished the task, Alex noticed he doesn't know the answer. Help him don't loose face in front of Valentina and solve the problem!

翻译中…

Input

翻译中…

The first line of the input contains an integer $*n*$ ($1 ≤ *n* ≤ 500 000$) — the length of the string $*s*$.

翻译中…

The second line contains a string $*s*$ of length $*n*$ consisting of only '(' and ')'.

翻译中…

Output

翻译中…

Print the number of distinct non-empty correct sequences that occur in $*s*$ as substring.

翻译中…

Note

翻译中…

In the first sample, there are $5$ distinct substrings we should count: "()", "()()", "()()()", "()()()()" and "()()()()()".

翻译中…

In the second sample, there are $3$ distinct substrings we should count: "()", "(())" and "(())()".

翻译中…

样例

Input #1
10
()()()()()
Output #1
5
InputCopy OutputCopy
Input #2
7
)(())()
Output #2
3
InputCopy OutputCopy
题目信息
题号
653F
时间限制
3 s
内存限制
512 MB
标签
数据结构
后缀结构
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。