#include <bits/stdc++.h>
using namespace std;
class SuffixArray {
 public:
  int *sa, *lcp, *rank, n;
  unsigned char *s;
  void getbuckets(unsigned char s[], vector<int> &bkt, int n, int k, int cs,
                  bool end) {
    for (int i = (0); i <= (k); i = i + 1) bkt[i] = 0;
    for (int i = 0; i < (n); i = i + 1)
      bkt[((cs == sizeof(int)) ? ((int *)s)[i] : ((unsigned char *)s)[i])]++;
    int sum = 0;
    for (int i = (0); i <= (k); i = i + 1) {
      sum += bkt[i];
      bkt[i] = end ? sum : sum - bkt[i];
    }
  }
  void inducesal(vector<unsigned char> &t, int sa[], unsigned char s[],
                 vector<int> &bkt, int n, int k, int cs, bool end) {
    getbuckets(s, bkt, n, k, cs, end);
    for (int i = 0; i < (n); i = i + 1) {
      int j = sa[i] - 1;
      if (j >= 0 && !((t[(j) / 8] & (1 << ((j) % 8))) ? 1 : 0))
        sa[bkt[((cs == sizeof(int)) ? ((int *)s)[j]
                                    : ((unsigned char *)s)[j])]++] = j;
    }
  }
  void inducesas(vector<unsigned char> &t, int sa[], unsigned char s[],
                 vector<int> &bkt, int n, int k, int cs, bool end) {
    getbuckets(s, bkt, n, k, cs, end);
    for (int i = (n - 1); i >= (0); i = i - 1) {
      int j = sa[i] - 1;
      if (j >= 0 && ((t[(j) / 8] & (1 << ((j) % 8))) ? 1 : 0))
        sa[--bkt[((cs == sizeof(int)) ? ((int *)s)[j]
                                      : ((unsigned char *)s)[j])]] = j;
    }
  }
  void build(unsigned char s[], int sa[], int n, int k, int cs) {
    int j;
    vector<unsigned char> t = vector<unsigned char>(n / 8 + 1, 0);
    t[(n - 2) / 8] = (0) ? (t[(n - 2) / 8] | (1 << ((n - 2) % 8)))
                         : (t[(n - 2) / 8] & (~(1 << ((n - 2) % 8))));
    t[(n - 1) / 8] = (1) ? (t[(n - 1) / 8] | (1 << ((n - 1) % 8)))
                         : (t[(n - 1) / 8] & (~(1 << ((n - 1) % 8))));
    for (int i = (n - 3); i >= (0); i = i - 1)
      t[(i) / 8] =
          ((((cs == sizeof(int)) ? ((int *)s)[i] : ((unsigned char *)s)[i]) <
                ((cs == sizeof(int)) ? ((int *)s)[i + 1]
                                     : ((unsigned char *)s)[i + 1]) ||
            (((cs == sizeof(int)) ? ((int *)s)[i] : ((unsigned char *)s)[i]) ==
                 ((cs == sizeof(int)) ? ((int *)s)[i + 1]
                                      : ((unsigned char *)s)[i + 1]) &&
             ((t[(i + 1) / 8] & (1 << ((i + 1) % 8))) ? 1 : 0)))
               ? 1
               : 0)
              ? (t[(i) / 8] | (1 << ((i) % 8)))
              : (t[(i) / 8] & (~(1 << ((i) % 8))));
    vector<int> bkt = vector<int>(k + 1, 0);
    getbuckets(s, bkt, n, k, cs, true);
    for (int i = 0; i < (n); i = i + 1) sa[i] = -1;
    for (int i = 0; i < (n); i = i + 1)
      if (((i) > 0 && ((t[(i) / 8] & (1 << ((i) % 8))) ? 1 : 0) &&
           !((t[((i)-1) / 8] & (1 << (((i)-1) % 8))) ? 1 : 0)))
        sa[--bkt[((cs == sizeof(int)) ? ((int *)s)[i]
                                      : ((unsigned char *)s)[i])]] = i;
    inducesal(t, sa, s, bkt, n, k, cs, false);
    inducesas(t, sa, s, bkt, n, k, cs, true);
    bkt.clear();
    int n1 = 0;
    for (int i = 0; i < (n); i = i + 1)
      if (((sa[i]) > 0 && ((t[(sa[i]) / 8] & (1 << ((sa[i]) % 8))) ? 1 : 0) &&
           !((t[((sa[i]) - 1) / 8] & (1 << (((sa[i]) - 1) % 8))) ? 1 : 0)))
        sa[n1++] = sa[i];
    for (int i = (n1); i <= (n - 1); i = i + 1) sa[i] = -1;
    int name = 0;
    int prev = -1;
    for (int i = 0; i < (n1); i = i + 1) {
      int pos = sa[i];
      bool diff = false;
      for (int d = 0; d < (n); d = d + 1) {
        if (prev < 0 ||
            ((cs == sizeof(int)) ? ((int *)s)[prev + d]
                                 : ((unsigned char *)s)[prev + d]) !=
                ((cs == sizeof(int)) ? ((int *)s)[pos + d]
                                     : ((unsigned char *)s)[pos + d]) ||
            ((t[(prev + d) / 8] & (1 << ((prev + d) % 8))) ? 1 : 0) !=
                ((t[(pos + d) / 8] & (1 << ((pos + d) % 8))) ? 1 : 0)) {
          diff = true;
          break;
        } else if (d > 0 &&
                   (((prev + d) > 0 &&
                     ((t[(prev + d) / 8] & (1 << ((prev + d) % 8))) ? 1 : 0) &&
                     !((t[((prev + d) - 1) / 8] & (1 << (((prev + d) - 1) % 8)))
                           ? 1
                           : 0)) ||
                    ((pos + d) > 0 &&
                     ((t[(pos + d) / 8] & (1 << ((pos + d) % 8))) ? 1 : 0) &&
                     !((t[((pos + d) - 1) / 8] & (1 << (((pos + d) - 1) % 8)))
                           ? 1
                           : 0))))
          break;
      }
      if (diff) {
        name++;
        prev = pos;
      }
      sa[n1 + pos / 2] = name - 1;
    }
    j = n - 1;
    for (int i = (n - 1); i >= (n1); i = i - 1)
      if (sa[i] >= 0) sa[j--] = sa[i];
    int *sa1 = sa;
    int *s1 = sa + n - n1;
    if (name < n1)
      build((unsigned char *)s1, sa1, n1, name - 1, sizeof(int));
    else
      for (int i = 0; i < (n1); i = i + 1) sa1[s1[i]] = i;
    bkt.assign(k + 1, 0);
    getbuckets(s, bkt, n, k, cs, true);
    j = 0;
    for (int i = 0; i < (n); i = i + 1)
      if (((i) > 0 && ((t[(i) / 8] & (1 << ((i) % 8))) ? 1 : 0) &&
           !((t[((i)-1) / 8] & (1 << (((i)-1) % 8))) ? 1 : 0)))
        s1[j++] = i;
    for (int i = 0; i < (n1); i = i + 1) sa1[i] = s1[sa1[i]];
    for (int i = (n1); i <= (n - 1); i = i + 1) sa[i] = -1;
    for (int i = (n1 - 1); i >= (0); i = i - 1) {
      j = sa[i];
      sa[i] = -1;
      sa[--bkt[((cs == sizeof(int)) ? ((int *)s)[j]
                                    : ((unsigned char *)s)[j])]] = j;
    }
    inducesal(t, sa, s, bkt, n, k, cs, false);
    inducesas(t, sa, s, bkt, n, k, cs, true);
    bkt.clear();
    t.clear();
  }
  void calc_lcp(void) {
    for (int i = (1); i <= (n); i = i + 1) rank[sa[i]] = i;
    int h = 0;
    for (int i = 0; i < (n); i = i + 1)
      if (rank[i] < n) {
        int j = sa[rank[i] + 1];
        while (s[i + h] == s[j + h]) h++;
        lcp[rank[i]] = h;
        if (h > 0) h--;
      }
  }
  SuffixArray() {
    n = 0;
    sa = NULL;
    lcp = NULL;
    rank = NULL;
    s = NULL;
  }
  SuffixArray(string ss) {
    n = ss.size();
    sa = new int[n + 7];
    lcp = new int[n + 7];
    rank = new int[n + 7];
    s = (unsigned char *)ss.c_str();
    build(s, sa, n + 1, 256, sizeof(char));
    calc_lcp();
  }
};
char s[500500];
int n, range[500500], sum[500500];
vector<int> haveSum[2 * 500500];
SuffixArray sa;
void init(void) {
  scanf("%d", &n);
  scanf("%s", s);
  for (int i = 0; i < (n); i = i + 1)
    sum[i] = (i == 0 ? 0 : sum[i - 1]) + (s[i] == '(' ? 1 : -1);
  for (int i = 0; i < (n); i = i + 1) haveSum[sum[i] + 500500].push_back(i);
  vector<pair<int, int> > v1, v2;
  for (int i = 0; i < (n); i = i + 1) {
    v1.push_back(make_pair(i == 0 ? 0 : sum[i - 1], i));
    v2.push_back(make_pair(sum[i], i));
  }
  sort((v1).begin(), (v1).end());
  sort((v2).begin(), (v2).end());
  set<int> pos;
  int j = 0;
  for (int i = 0; i < (n); i = i + 1) {
    while (j < v2.size() && v2[j].first < v1[i].first) {
      pos.insert(v2[j].second);
      j++;
    }
    __typeof(pos.begin()) it = pos.lower_bound(v1[i].second);
    range[v1[i].second] = it == pos.end() ? n - 1 : *it - 1;
  }
  sa = SuffixArray(string(s));
}
void process(void) {
  long long res = 0;
  for (int i = (1); i <= (n); i = i + 1) {
    int cur = sa.sa[i];
    int maxRange = range[cur];
    int minRange = i == 1 ? cur : cur + sa.lcp[i - 1];
    if (maxRange < minRange) continue;
    int tmp = cur == 0 ? 0 : sum[cur - 1];
    int L = lower_bound((haveSum[tmp + 500500]).begin(),
                        (haveSum[tmp + 500500]).end(), minRange) -
            haveSum[tmp + 500500].begin();
    int R = upper_bound((haveSum[tmp + 500500]).begin(),
                        (haveSum[tmp + 500500]).end(), maxRange) -
            haveSum[tmp + 500500].begin();
    if (R >= L) res += R - L;
  }
  cout << res << endl;
}
int main(void) {
  init();
  process();
  return 0;
}

### Prompt

Please formulate a Cpp solution to the following problem:

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.

Examples

Input

10
()()()()()


Output

5


Input

7
)(())()


Output

3

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 "(())()".

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
class SuffixArray {
 public:
  int *sa, *lcp, *rank, n;
  unsigned char *s;
  void getbuckets(unsigned char s[], vector<int> &bkt, int n, int k, int cs,
                  bool end) {
    for (int i = (0); i <= (k); i = i + 1) bkt[i] = 0;
    for (int i = 0; i < (n); i = i + 1)
      bkt[((cs == sizeof(int)) ? ((int *)s)[i] : ((unsigned char *)s)[i])]++;
    int sum = 0;
    for (int i = (0); i <= (k); i = i + 1) {
      sum += bkt[i];
      bkt[i] = end ? sum : sum - bkt[i];
    }
  }
  void inducesal(vector<unsigned char> &t, int sa[], unsigned char s[],
                 vector<int> &bkt, int n, int k, int cs, bool end) {
    getbuckets(s, bkt, n, k, cs, end);
    for (int i = 0; i < (n); i = i + 1) {
      int j = sa[i] - 1;
      if (j >= 0 && !((t[(j) / 8] & (1 << ((j) % 8))) ? 1 : 0))
        sa[bkt[((cs == sizeof(int)) ? ((int *)s)[j]
                                    : ((unsigned char *)s)[j])]++] = j;
    }
  }
  void inducesas(vector<unsigned char> &t, int sa[], unsigned char s[],
                 vector<int> &bkt, int n, int k, int cs, bool end) {
    getbuckets(s, bkt, n, k, cs, end);
    for (int i = (n - 1); i >= (0); i = i - 1) {
      int j = sa[i] - 1;
      if (j >= 0 && ((t[(j) / 8] & (1 << ((j) % 8))) ? 1 : 0))
        sa[--bkt[((cs == sizeof(int)) ? ((int *)s)[j]
                                      : ((unsigned char *)s)[j])]] = j;
    }
  }
  void build(unsigned char s[], int sa[], int n, int k, int cs) {
    int j;
    vector<unsigned char> t = vector<unsigned char>(n / 8 + 1, 0);
    t[(n - 2) / 8] = (0) ? (t[(n - 2) / 8] | (1 << ((n - 2) % 8)))
                         : (t[(n - 2) / 8] & (~(1 << ((n - 2) % 8))));
    t[(n - 1) / 8] = (1) ? (t[(n - 1) / 8] | (1 << ((n - 1) % 8)))
                         : (t[(n - 1) / 8] & (~(1 << ((n - 1) % 8))));
    for (int i = (n - 3); i >= (0); i = i - 1)
      t[(i) / 8] =
          ((((cs == sizeof(int)) ? ((int *)s)[i] : ((unsigned char *)s)[i]) <
                ((cs == sizeof(int)) ? ((int *)s)[i + 1]
                                     : ((unsigned char *)s)[i + 1]) ||
            (((cs == sizeof(int)) ? ((int *)s)[i] : ((unsigned char *)s)[i]) ==
                 ((cs == sizeof(int)) ? ((int *)s)[i + 1]
                                      : ((unsigned char *)s)[i + 1]) &&
             ((t[(i + 1) / 8] & (1 << ((i + 1) % 8))) ? 1 : 0)))
               ? 1
               : 0)
              ? (t[(i) / 8] | (1 << ((i) % 8)))
              : (t[(i) / 8] & (~(1 << ((i) % 8))));
    vector<int> bkt = vector<int>(k + 1, 0);
    getbuckets(s, bkt, n, k, cs, true);
    for (int i = 0; i < (n); i = i + 1) sa[i] = -1;
    for (int i = 0; i < (n); i = i + 1)
      if (((i) > 0 && ((t[(i) / 8] & (1 << ((i) % 8))) ? 1 : 0) &&
           !((t[((i)-1) / 8] & (1 << (((i)-1) % 8))) ? 1 : 0)))
        sa[--bkt[((cs == sizeof(int)) ? ((int *)s)[i]
                                      : ((unsigned char *)s)[i])]] = i;
    inducesal(t, sa, s, bkt, n, k, cs, false);
    inducesas(t, sa, s, bkt, n, k, cs, true);
    bkt.clear();
    int n1 = 0;
    for (int i = 0; i < (n); i = i + 1)
      if (((sa[i]) > 0 && ((t[(sa[i]) / 8] & (1 << ((sa[i]) % 8))) ? 1 : 0) &&
           !((t[((sa[i]) - 1) / 8] & (1 << (((sa[i]) - 1) % 8))) ? 1 : 0)))
        sa[n1++] = sa[i];
    for (int i = (n1); i <= (n - 1); i = i + 1) sa[i] = -1;
    int name = 0;
    int prev = -1;
    for (int i = 0; i < (n1); i = i + 1) {
      int pos = sa[i];
      bool diff = false;
      for (int d = 0; d < (n); d = d + 1) {
        if (prev < 0 ||
            ((cs == sizeof(int)) ? ((int *)s)[prev + d]
                                 : ((unsigned char *)s)[prev + d]) !=
                ((cs == sizeof(int)) ? ((int *)s)[pos + d]
                                     : ((unsigned char *)s)[pos + d]) ||
            ((t[(prev + d) / 8] & (1 << ((prev + d) % 8))) ? 1 : 0) !=
                ((t[(pos + d) / 8] & (1 << ((pos + d) % 8))) ? 1 : 0)) {
          diff = true;
          break;
        } else if (d > 0 &&
                   (((prev + d) > 0 &&
                     ((t[(prev + d) / 8] & (1 << ((prev + d) % 8))) ? 1 : 0) &&
                     !((t[((prev + d) - 1) / 8] & (1 << (((prev + d) - 1) % 8)))
                           ? 1
                           : 0)) ||
                    ((pos + d) > 0 &&
                     ((t[(pos + d) / 8] & (1 << ((pos + d) % 8))) ? 1 : 0) &&
                     !((t[((pos + d) - 1) / 8] & (1 << (((pos + d) - 1) % 8)))
                           ? 1
                           : 0))))
          break;
      }
      if (diff) {
        name++;
        prev = pos;
      }
      sa[n1 + pos / 2] = name - 1;
    }
    j = n - 1;
    for (int i = (n - 1); i >= (n1); i = i - 1)
      if (sa[i] >= 0) sa[j--] = sa[i];
    int *sa1 = sa;
    int *s1 = sa + n - n1;
    if (name < n1)
      build((unsigned char *)s1, sa1, n1, name - 1, sizeof(int));
    else
      for (int i = 0; i < (n1); i = i + 1) sa1[s1[i]] = i;
    bkt.assign(k + 1, 0);
    getbuckets(s, bkt, n, k, cs, true);
    j = 0;
    for (int i = 0; i < (n); i = i + 1)
      if (((i) > 0 && ((t[(i) / 8] & (1 << ((i) % 8))) ? 1 : 0) &&
           !((t[((i)-1) / 8] & (1 << (((i)-1) % 8))) ? 1 : 0)))
        s1[j++] = i;
    for (int i = 0; i < (n1); i = i + 1) sa1[i] = s1[sa1[i]];
    for (int i = (n1); i <= (n - 1); i = i + 1) sa[i] = -1;
    for (int i = (n1 - 1); i >= (0); i = i - 1) {
      j = sa[i];
      sa[i] = -1;
      sa[--bkt[((cs == sizeof(int)) ? ((int *)s)[j]
                                    : ((unsigned char *)s)[j])]] = j;
    }
    inducesal(t, sa, s, bkt, n, k, cs, false);
    inducesas(t, sa, s, bkt, n, k, cs, true);
    bkt.clear();
    t.clear();
  }
  void calc_lcp(void) {
    for (int i = (1); i <= (n); i = i + 1) rank[sa[i]] = i;
    int h = 0;
    for (int i = 0; i < (n); i = i + 1)
      if (rank[i] < n) {
        int j = sa[rank[i] + 1];
        while (s[i + h] == s[j + h]) h++;
        lcp[rank[i]] = h;
        if (h > 0) h--;
      }
  }
  SuffixArray() {
    n = 0;
    sa = NULL;
    lcp = NULL;
    rank = NULL;
    s = NULL;
  }
  SuffixArray(string ss) {
    n = ss.size();
    sa = new int[n + 7];
    lcp = new int[n + 7];
    rank = new int[n + 7];
    s = (unsigned char *)ss.c_str();
    build(s, sa, n + 1, 256, sizeof(char));
    calc_lcp();
  }
};
char s[500500];
int n, range[500500], sum[500500];
vector<int> haveSum[2 * 500500];
SuffixArray sa;
void init(void) {
  scanf("%d", &n);
  scanf("%s", s);
  for (int i = 0; i < (n); i = i + 1)
    sum[i] = (i == 0 ? 0 : sum[i - 1]) + (s[i] == '(' ? 1 : -1);
  for (int i = 0; i < (n); i = i + 1) haveSum[sum[i] + 500500].push_back(i);
  vector<pair<int, int> > v1, v2;
  for (int i = 0; i < (n); i = i + 1) {
    v1.push_back(make_pair(i == 0 ? 0 : sum[i - 1], i));
    v2.push_back(make_pair(sum[i], i));
  }
  sort((v1).begin(), (v1).end());
  sort((v2).begin(), (v2).end());
  set<int> pos;
  int j = 0;
  for (int i = 0; i < (n); i = i + 1) {
    while (j < v2.size() && v2[j].first < v1[i].first) {
      pos.insert(v2[j].second);
      j++;
    }
    __typeof(pos.begin()) it = pos.lower_bound(v1[i].second);
    range[v1[i].second] = it == pos.end() ? n - 1 : *it - 1;
  }
  sa = SuffixArray(string(s));
}
void process(void) {
  long long res = 0;
  for (int i = (1); i <= (n); i = i + 1) {
    int cur = sa.sa[i];
    int maxRange = range[cur];
    int minRange = i == 1 ? cur : cur + sa.lcp[i - 1];
    if (maxRange < minRange) continue;
    int tmp = cur == 0 ? 0 : sum[cur - 1];
    int L = lower_bound((haveSum[tmp + 500500]).begin(),
                        (haveSum[tmp + 500500]).end(), minRange) -
            haveSum[tmp + 500500].begin();
    int R = upper_bound((haveSum[tmp + 500500]).begin(),
                        (haveSum[tmp + 500500]).end(), maxRange) -
            haveSum[tmp + 500500].begin();
    if (R >= L) res += R - L;
  }
  cout << res << endl;
}
int main(void) {
  init();
  process();
  return 0;
}

```