#include <bits/stdc++.h>
#pragma GCC optimize(3)
#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
using namespace std;
struct segt {
  int rmin, cnt;
  segt operator+(segt other) const {
    segt ret;
    ret.rmin = min(rmin, other.rmin);
    ret.cnt = (ret.rmin == rmin) * cnt + (ret.rmin == other.rmin) * other.cnt;
    return ret;
  }
};
struct SA {
  int p[524292];
  int cnt[524292];
  int c[20][524292];
  int logs[524292];
  int rank[524292];
  const int alphabet = 256;
  int n;
  void init() {
    memset(p, 0, sizeof(p));
    memset(cnt, 0, sizeof(cnt));
    memset(c, 0, sizeof(c));
  }
  void sort_cyclic_shifts(string &s) {
    int h, i;
    n = (int)s.size();
    for (i = 2; i < 524292; ++i) logs[i] = logs[i / 2] + 1;
    for (i = 0; i < n; i++) cnt[(int)s[i]]++;
    for (i = 1; i < alphabet; i++) cnt[i] += cnt[i - 1];
    for (i = 0; i < n; i++) p[--cnt[(int)s[i]]] = i;
    c[0][p[0]] = 0;
    int classes = 1;
    for (i = 1; i < n; i++) {
      if (s[p[i]] != s[p[i - 1]]) classes++;
      c[0][p[i]] = classes - 1;
    }
    vector<int> pn(n), cn(n);
    for (h = 0; (1 << h) < n; ++h) {
      for (i = 0; i < n; i++) {
        pn[i] = p[i] - (1 << h);
        if (pn[i] < 0) pn[i] += n;
      }
      fill(cnt, cnt + classes, 0);
      for (i = 0; i < n; i++) cnt[c[h][pn[i]]]++;
      for (i = 1; i < classes; i++) cnt[i] += cnt[i - 1];
      for (i = n - 1; i >= 0; i--) p[--cnt[c[h][pn[i]]]] = pn[i];
      cn[p[0]] = 0;
      classes = 1;
      for (i = 1; i < n; i++) {
        pair<int, int> cur = {c[h][p[i]], c[h][(p[i] + (1 << h)) % n]};
        pair<int, int> prev = {c[h][p[i - 1]], c[h][(p[i - 1] + (1 << h)) % n]};
        if (cur != prev) ++classes;
        cn[p[i]] = classes - 1;
      }
      for (i = 0; i < n; ++i) {
        c[h + 1][i] = cn[i];
      }
    }
    for (i = 0; i < n; ++i) {
      rank[p[i]] = i;
    }
  }
  int lcp(int i, int j) {
    int ans = 0, k;
    for (k = logs[n]; k >= 0; k--) {
      if (c[k][i] == c[k][j]) {
        ans += 1 << k;
        i += 1 << k;
        j += 1 << k;
      }
    }
    return ans;
  }
};
SA sa;
segt tree[524292 << 1];
int n;
int prefix[524292];
char st[524292];
string s;
void build(int l, int r, int index) {
  if (l > r) return;
  if (l == r) {
    tree[index].rmin = prefix[l];
    tree[index].cnt = 1;
    return;
  }
  build(l, ((l + r) >> 1), (index << 1));
  build(((l + r) >> 1) + 1, r, (index << 1 | 1));
  tree[index] = tree[(index << 1)] + tree[(index << 1 | 1)];
}
int search(int start, int end, int l, int r, int index, int val) {
  if (start > end || l > r || start > r || l > end) return 524292;
  if (tree[index].rmin >= val) return 524292;
  if (l == r) return l;
  if (start <= l && r <= end) {
    if (tree[(index << 1)].rmin < val)
      return search(start, end, l, ((l + r) >> 1), (index << 1), val);
    return search(start, end, ((l + r) >> 1) + 1, r, (index << 1 | 1), val);
  }
  if (end <= ((l + r) >> 1))
    return search(start, end, l, ((l + r) >> 1), (index << 1), val);
  if (start >= ((l + r) >> 1) + 1)
    return search(start, end, ((l + r) >> 1) + 1, r, (index << 1 | 1), val);
  return min(search(start, end, l, ((l + r) >> 1), (index << 1), val),
             search(start, end, ((l + r) >> 1) + 1, r, (index << 1 | 1), val));
}
segt query(int start, int end, int l, int r, int index) {
  if (start > end || l > r || start > r || l > end) return tree[0];
  if (start <= l && r <= end) return tree[index];
  if (end <= ((l + r) >> 1))
    return query(start, end, l, ((l + r) >> 1), (index << 1));
  if (start >= ((l + r) >> 1) + 1)
    return query(start, end, ((l + r) >> 1) + 1, r, (index << 1 | 1));
  return query(start, end, l, ((l + r) >> 1), (index << 1)) +
         query(start, end, ((l + r) >> 1) + 1, r, (index << 1 | 1));
}
int val(char ch) { return ch == '(' ? 1 : -1; }
int getans(int l, int r) {
  int L = l > 0 ? prefix[l - 1] : 0;
  int pos = search(l, r, 0, n - 1, 1, L);
  if (pos < n) {
    auto ret = query(l, pos - 1, 0, n - 1, 1);
    if (ret.rmin == L) return ret.cnt;
    return 0;
  }
  auto ret = query(l, r, 0, n - 1, 1);
  if (ret.rmin == L) return ret.cnt;
  return 0;
}
int main() {
  int i;
  long long ans = 0;
  scanf("%d", &n);
  scanf("%s", st);
  s = string(st);
  s += '$';
  sa.sort_cyclic_shifts(s);
  prefix[0] = val(st[0]);
  for (i = 1; i < n; ++i) prefix[i] = prefix[i - 1] + val(st[i]);
  build(0, n - 1, 1);
  for (i = 0; i < n; ++i) {
    ans += getans(i, n - 1);
  }
  for (i = 0; i < sa.n - 1; ++i) {
    int lcp = sa.lcp(sa.p[i], sa.p[i + 1]);
    ans -= getans(sa.p[i], sa.p[i] + lcp - 1);
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Construct a Cpp code solution to the problem outlined:

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>
#pragma GCC optimize(3)
#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
using namespace std;
struct segt {
  int rmin, cnt;
  segt operator+(segt other) const {
    segt ret;
    ret.rmin = min(rmin, other.rmin);
    ret.cnt = (ret.rmin == rmin) * cnt + (ret.rmin == other.rmin) * other.cnt;
    return ret;
  }
};
struct SA {
  int p[524292];
  int cnt[524292];
  int c[20][524292];
  int logs[524292];
  int rank[524292];
  const int alphabet = 256;
  int n;
  void init() {
    memset(p, 0, sizeof(p));
    memset(cnt, 0, sizeof(cnt));
    memset(c, 0, sizeof(c));
  }
  void sort_cyclic_shifts(string &s) {
    int h, i;
    n = (int)s.size();
    for (i = 2; i < 524292; ++i) logs[i] = logs[i / 2] + 1;
    for (i = 0; i < n; i++) cnt[(int)s[i]]++;
    for (i = 1; i < alphabet; i++) cnt[i] += cnt[i - 1];
    for (i = 0; i < n; i++) p[--cnt[(int)s[i]]] = i;
    c[0][p[0]] = 0;
    int classes = 1;
    for (i = 1; i < n; i++) {
      if (s[p[i]] != s[p[i - 1]]) classes++;
      c[0][p[i]] = classes - 1;
    }
    vector<int> pn(n), cn(n);
    for (h = 0; (1 << h) < n; ++h) {
      for (i = 0; i < n; i++) {
        pn[i] = p[i] - (1 << h);
        if (pn[i] < 0) pn[i] += n;
      }
      fill(cnt, cnt + classes, 0);
      for (i = 0; i < n; i++) cnt[c[h][pn[i]]]++;
      for (i = 1; i < classes; i++) cnt[i] += cnt[i - 1];
      for (i = n - 1; i >= 0; i--) p[--cnt[c[h][pn[i]]]] = pn[i];
      cn[p[0]] = 0;
      classes = 1;
      for (i = 1; i < n; i++) {
        pair<int, int> cur = {c[h][p[i]], c[h][(p[i] + (1 << h)) % n]};
        pair<int, int> prev = {c[h][p[i - 1]], c[h][(p[i - 1] + (1 << h)) % n]};
        if (cur != prev) ++classes;
        cn[p[i]] = classes - 1;
      }
      for (i = 0; i < n; ++i) {
        c[h + 1][i] = cn[i];
      }
    }
    for (i = 0; i < n; ++i) {
      rank[p[i]] = i;
    }
  }
  int lcp(int i, int j) {
    int ans = 0, k;
    for (k = logs[n]; k >= 0; k--) {
      if (c[k][i] == c[k][j]) {
        ans += 1 << k;
        i += 1 << k;
        j += 1 << k;
      }
    }
    return ans;
  }
};
SA sa;
segt tree[524292 << 1];
int n;
int prefix[524292];
char st[524292];
string s;
void build(int l, int r, int index) {
  if (l > r) return;
  if (l == r) {
    tree[index].rmin = prefix[l];
    tree[index].cnt = 1;
    return;
  }
  build(l, ((l + r) >> 1), (index << 1));
  build(((l + r) >> 1) + 1, r, (index << 1 | 1));
  tree[index] = tree[(index << 1)] + tree[(index << 1 | 1)];
}
int search(int start, int end, int l, int r, int index, int val) {
  if (start > end || l > r || start > r || l > end) return 524292;
  if (tree[index].rmin >= val) return 524292;
  if (l == r) return l;
  if (start <= l && r <= end) {
    if (tree[(index << 1)].rmin < val)
      return search(start, end, l, ((l + r) >> 1), (index << 1), val);
    return search(start, end, ((l + r) >> 1) + 1, r, (index << 1 | 1), val);
  }
  if (end <= ((l + r) >> 1))
    return search(start, end, l, ((l + r) >> 1), (index << 1), val);
  if (start >= ((l + r) >> 1) + 1)
    return search(start, end, ((l + r) >> 1) + 1, r, (index << 1 | 1), val);
  return min(search(start, end, l, ((l + r) >> 1), (index << 1), val),
             search(start, end, ((l + r) >> 1) + 1, r, (index << 1 | 1), val));
}
segt query(int start, int end, int l, int r, int index) {
  if (start > end || l > r || start > r || l > end) return tree[0];
  if (start <= l && r <= end) return tree[index];
  if (end <= ((l + r) >> 1))
    return query(start, end, l, ((l + r) >> 1), (index << 1));
  if (start >= ((l + r) >> 1) + 1)
    return query(start, end, ((l + r) >> 1) + 1, r, (index << 1 | 1));
  return query(start, end, l, ((l + r) >> 1), (index << 1)) +
         query(start, end, ((l + r) >> 1) + 1, r, (index << 1 | 1));
}
int val(char ch) { return ch == '(' ? 1 : -1; }
int getans(int l, int r) {
  int L = l > 0 ? prefix[l - 1] : 0;
  int pos = search(l, r, 0, n - 1, 1, L);
  if (pos < n) {
    auto ret = query(l, pos - 1, 0, n - 1, 1);
    if (ret.rmin == L) return ret.cnt;
    return 0;
  }
  auto ret = query(l, r, 0, n - 1, 1);
  if (ret.rmin == L) return ret.cnt;
  return 0;
}
int main() {
  int i;
  long long ans = 0;
  scanf("%d", &n);
  scanf("%s", st);
  s = string(st);
  s += '$';
  sa.sort_cyclic_shifts(s);
  prefix[0] = val(st[0]);
  for (i = 1; i < n; ++i) prefix[i] = prefix[i - 1] + val(st[i]);
  build(0, n - 1, 1);
  for (i = 0; i < n; ++i) {
    ans += getans(i, n - 1);
  }
  for (i = 0; i < sa.n - 1; ++i) {
    int lcp = sa.lcp(sa.p[i], sa.p[i + 1]);
    ans -= getans(sa.p[i], sa.p[i] + lcp - 1);
  }
  cout << ans << endl;
  return 0;
}

```