Balanced Paths

Count ordered node pairs whose labels along the tree path form a balanced parenthesis string.

Hard8Divide and conquerHash mapPrefix sumTreeNo attempts yetTime limit3sMemory limit256 MB

Problem

You are given an undirected tree with nn nodes, numbered 11 through nn. Every node is labeled with either ( or ). For two nodes uu and vv, let l[uv]l[u \to v] be the string obtained by concatenating the labels of the nodes on the simple path from uu to vv, read in order from uu to vv. On a tree the simple path between two nodes is unique.

A balanced string is defined as follows.

  • The empty string is balanced.
  • If ss is balanced, then the concatenation of (, ss, ) is balanced.
  • If ss and tt are balanced, then their concatenation stst is balanced.
  • No other string is balanced.

Count the ordered pairs of nodes (u,v)(u, v) such that l[uv]l[u \to v] is balanced.

Input

The first line contains an integer nn (2n1000002 \le n \le 100000), the number of nodes of the tree.

The second line contains a string of length nn. Each character of the string is ( or ), and the xx-th character is the label of node xx.

Each of the next n1n - 1 lines contains two integers aia_i and bib_i (1ai,bin1 \le a_i, b_i \le n), meaning that node aia_i and node bib_i are joined by an edge. The given graph is a tree.

Output

Print one line containing the number of ordered pairs (u,v)(u, v) such that l[uv]l[u \to v] is balanced.