#include <bits/stdc++.h>
using namespace std;
struct Segment {
  Segment(int l, int r)
      : left(l), right(r), value(0), leftchild(nullptr), rightchild(nullptr) {}
  int left;
  int right;
  int value;
  Segment *leftchild;
  Segment *rightchild;
};
static Segment *build(int left, int right, int *arr) {
  Segment *x = new Segment(left, right);
  if (left < right) {
    int mid = (left + right) / 2;
    x->leftchild = build(left, mid, arr);
    x->rightchild = build(mid + 1, right, arr);
    if (arr) x->value = min(x->leftchild->value, x->rightchild->value);
  } else {
    if (arr) x->value = arr[left];
  }
  return x;
}
static int query_min(Segment *x, int left, int right) {
  if (left <= x->left && x->right <= right)
    return x->value;
  else {
    int mid = (x->left + x->right) / 2;
    int answer = INT_MAX;
    if (left <= mid) answer = min(answer, query_min(x->leftchild, left, right));
    if (right > mid)
      answer = min(answer, query_min(x->rightchild, left, right));
    return answer;
  }
}
static int find_first(Segment *x, int left, int right, int std) {
  if (x->left == x->right)
    return x->left;
  else {
    int mid = (x->left + x->right) / 2;
    if (query_min(x->leftchild, left, mid) < std)
      return find_first(x->leftchild, left, right, std);
    else
      return find_first(x->rightchild, left, right, std);
  }
}
static Segment *persistent_modify(Segment *x, int pos, int delta) {
  x = new Segment(*x);
  if (x->left == x->right) {
    x->value += delta;
  } else {
    int mid = (x->left + x->right) / 2;
    if (pos <= mid)
      x->leftchild = persistent_modify(x->leftchild, pos, delta);
    else
      x->rightchild = persistent_modify(x->rightchild, pos, delta);
  }
  return x;
}
static int query(Segment *x, int pos) {
  if (x->left == x->right)
    return x->value;
  else {
    int mid = (x->left + x->right) / 2;
    if (pos <= mid)
      return query(x->leftchild, pos);
    else
      return query(x->rightchild, pos);
  }
}
struct Node {
  struct Edge {
    Edge(int l, int r, Node *nxt) : left(l), right(r), next(nxt) {}
    int left;
    int right;
    Node *next;
  };
  Node() : suffix_link(nullptr) { memset(trans, 0, sizeof(trans)); }
  Node *suffix_link;
  Edge *trans[3];
};
static int n;
static int size;
static char str[500000 + 10];
inline int length(const Node::Edge *e) {
  return e->right ? e->right - e->left + 1 : size - e->left + 1;
}
static void append(Node *root, char c) {
  static Node *active = root;
  static char dir = -1;
  static int len = 0, cnt = 0;
  cnt++;
  Node *last = nullptr;
  while (cnt) {
    if (cnt - 1 < len) {
      len = cnt - 1;
      dir = len ? str[size - len + 1] : -1;
    }
    while (dir >= 0 && len >= length(active->trans[dir])) {
      len -= length(active->trans[dir]);
      active = active->trans[dir]->next;
      dir = len ? str[size - len + 1] : -1;
    }
    if ((dir < 0 && active->trans[c]) ||
        (dir >= 0 && c == str[active->trans[dir]->left + len])) {
      if (dir < 0) dir = c;
      len++;
      if (last) last->suffix_link = active;
      break;
    }
    Node *x;
    if (dir < 0) {
      active->trans[c] = new Node::Edge(size + 1, 0, new Node);
      x = active;
    } else {
      Node::Edge *t = active->trans[dir];
      x = new Node;
      x->trans[c] = new Node::Edge(size + 1, 0, new Node);
      x->trans[str[t->left + len]] =
          new Node::Edge(t->left + len, t->right, t->next);
      t->right = t->left + len - 1;
      t->next = x;
    }
    if (last) last->suffix_link = x;
    last = x;
    active = active->suffix_link ? active->suffix_link : root;
    cnt--;
  }
  size++;
}
static void fix(Node *x) {
  for (char c = 0; c < 3; c++) {
    Node::Edge *t = x->trans[c];
    if (t) {
      if (!t->right) t->right = size;
      fix(t->next);
    }
  }
}
inline int code(char c) {
  if (c == '(') return 1;
  return -1;
}
static int pre[500000 + 10];
static Segment *seg;
static Segment *version[500000 + 10];
static long long dfs(Node *x, int sum = 0) {
  long long answer = 0;
  for (char c = 0; c < 3; c++) {
    Node::Edge *t = x->trans[c];
    if (!t) continue;
    int delta = pre[t->left - 1];
    if (sum + query_min(seg, t->left, t->right) - delta >= 0) {
      answer += query(version[t->right], n - sum + delta);
      answer -= query(version[t->left - 1], n - sum + delta);
    } else {
      int p = find_first(seg, t->left, t->right, delta - sum);
      answer += query(version[p - 1], n - sum + delta);
      answer -= query(version[t->left - 1], n - sum + delta);
      continue;
    }
    answer += dfs(t->next, sum + pre[t->right] - pre[t->left - 1]);
  }
  return answer;
}
int main() {
  scanf("%d%s", &n, str + 1);
  for (int i = 1; i <= n; i++) {
    pre[i] = pre[i - 1] + code(str[i]);
  }
  pre[n + 1] = n * 2 + 1;
  seg = build(1, n + 1, pre);
  version[0] = build(0, n * 2 + 1, nullptr);
  for (int i = 1; i <= n + 1; i++) {
    version[i] = persistent_modify(version[i - 1], pre[i] + n, 1);
  }
  Node *tree = new Node;
  for (int i = 1; i <= n; i++) {
    str[i] -= '(';
    append(tree, str[i]);
  }
  append(tree, 2);
  fix(tree);
  printf("%lld\n", dfs(tree));
  return 0;
}

### Prompt

Create a solution in Cpp for 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;
struct Segment {
  Segment(int l, int r)
      : left(l), right(r), value(0), leftchild(nullptr), rightchild(nullptr) {}
  int left;
  int right;
  int value;
  Segment *leftchild;
  Segment *rightchild;
};
static Segment *build(int left, int right, int *arr) {
  Segment *x = new Segment(left, right);
  if (left < right) {
    int mid = (left + right) / 2;
    x->leftchild = build(left, mid, arr);
    x->rightchild = build(mid + 1, right, arr);
    if (arr) x->value = min(x->leftchild->value, x->rightchild->value);
  } else {
    if (arr) x->value = arr[left];
  }
  return x;
}
static int query_min(Segment *x, int left, int right) {
  if (left <= x->left && x->right <= right)
    return x->value;
  else {
    int mid = (x->left + x->right) / 2;
    int answer = INT_MAX;
    if (left <= mid) answer = min(answer, query_min(x->leftchild, left, right));
    if (right > mid)
      answer = min(answer, query_min(x->rightchild, left, right));
    return answer;
  }
}
static int find_first(Segment *x, int left, int right, int std) {
  if (x->left == x->right)
    return x->left;
  else {
    int mid = (x->left + x->right) / 2;
    if (query_min(x->leftchild, left, mid) < std)
      return find_first(x->leftchild, left, right, std);
    else
      return find_first(x->rightchild, left, right, std);
  }
}
static Segment *persistent_modify(Segment *x, int pos, int delta) {
  x = new Segment(*x);
  if (x->left == x->right) {
    x->value += delta;
  } else {
    int mid = (x->left + x->right) / 2;
    if (pos <= mid)
      x->leftchild = persistent_modify(x->leftchild, pos, delta);
    else
      x->rightchild = persistent_modify(x->rightchild, pos, delta);
  }
  return x;
}
static int query(Segment *x, int pos) {
  if (x->left == x->right)
    return x->value;
  else {
    int mid = (x->left + x->right) / 2;
    if (pos <= mid)
      return query(x->leftchild, pos);
    else
      return query(x->rightchild, pos);
  }
}
struct Node {
  struct Edge {
    Edge(int l, int r, Node *nxt) : left(l), right(r), next(nxt) {}
    int left;
    int right;
    Node *next;
  };
  Node() : suffix_link(nullptr) { memset(trans, 0, sizeof(trans)); }
  Node *suffix_link;
  Edge *trans[3];
};
static int n;
static int size;
static char str[500000 + 10];
inline int length(const Node::Edge *e) {
  return e->right ? e->right - e->left + 1 : size - e->left + 1;
}
static void append(Node *root, char c) {
  static Node *active = root;
  static char dir = -1;
  static int len = 0, cnt = 0;
  cnt++;
  Node *last = nullptr;
  while (cnt) {
    if (cnt - 1 < len) {
      len = cnt - 1;
      dir = len ? str[size - len + 1] : -1;
    }
    while (dir >= 0 && len >= length(active->trans[dir])) {
      len -= length(active->trans[dir]);
      active = active->trans[dir]->next;
      dir = len ? str[size - len + 1] : -1;
    }
    if ((dir < 0 && active->trans[c]) ||
        (dir >= 0 && c == str[active->trans[dir]->left + len])) {
      if (dir < 0) dir = c;
      len++;
      if (last) last->suffix_link = active;
      break;
    }
    Node *x;
    if (dir < 0) {
      active->trans[c] = new Node::Edge(size + 1, 0, new Node);
      x = active;
    } else {
      Node::Edge *t = active->trans[dir];
      x = new Node;
      x->trans[c] = new Node::Edge(size + 1, 0, new Node);
      x->trans[str[t->left + len]] =
          new Node::Edge(t->left + len, t->right, t->next);
      t->right = t->left + len - 1;
      t->next = x;
    }
    if (last) last->suffix_link = x;
    last = x;
    active = active->suffix_link ? active->suffix_link : root;
    cnt--;
  }
  size++;
}
static void fix(Node *x) {
  for (char c = 0; c < 3; c++) {
    Node::Edge *t = x->trans[c];
    if (t) {
      if (!t->right) t->right = size;
      fix(t->next);
    }
  }
}
inline int code(char c) {
  if (c == '(') return 1;
  return -1;
}
static int pre[500000 + 10];
static Segment *seg;
static Segment *version[500000 + 10];
static long long dfs(Node *x, int sum = 0) {
  long long answer = 0;
  for (char c = 0; c < 3; c++) {
    Node::Edge *t = x->trans[c];
    if (!t) continue;
    int delta = pre[t->left - 1];
    if (sum + query_min(seg, t->left, t->right) - delta >= 0) {
      answer += query(version[t->right], n - sum + delta);
      answer -= query(version[t->left - 1], n - sum + delta);
    } else {
      int p = find_first(seg, t->left, t->right, delta - sum);
      answer += query(version[p - 1], n - sum + delta);
      answer -= query(version[t->left - 1], n - sum + delta);
      continue;
    }
    answer += dfs(t->next, sum + pre[t->right] - pre[t->left - 1]);
  }
  return answer;
}
int main() {
  scanf("%d%s", &n, str + 1);
  for (int i = 1; i <= n; i++) {
    pre[i] = pre[i - 1] + code(str[i]);
  }
  pre[n + 1] = n * 2 + 1;
  seg = build(1, n + 1, pre);
  version[0] = build(0, n * 2 + 1, nullptr);
  for (int i = 1; i <= n + 1; i++) {
    version[i] = persistent_modify(version[i - 1], pre[i] + n, 1);
  }
  Node *tree = new Node;
  for (int i = 1; i <= n; i++) {
    str[i] -= '(';
    append(tree, str[i]);
  }
  append(tree, 2);
  fix(tree);
  printf("%lld\n", dfs(tree));
  return 0;
}

```