Under construction

Still being built. Things may change

Parentheses Tree

Time limit1sMemory limit1024 MB

Summary
Given a balanced parentheses string that encodes a rooted ordered tree, sum the distances from the root to every leaf.
Level

Easy3 of 10

Topics
String, Tree, Implementation
Solved
No attempts yet

Statement

A rooted ordered tree TT can be written as a string of matched parentheses p(T)p(T). A tree with a single node is written as (). When the root of TT has kk ordered subtrees T_1,T_2,…,T_kT\_1, T\_2, \dots , T\_k attached as its children, p(T)p(T) is defined as follows:

p(T)≔p(T) ≔ ( +p(T_1)+p(T_2)+⋯+p(T_k)++ p(T\_1) + p(T\_2) + \cdots + p(T\_k) + )

Here ++ means concatenation of two strings. The figure below shows two rooted ordered trees. The string representations p(T_L)p(T\_L) and p(T_R)p(T\_R) are ((()()())()) and (()((()(()))())), respectively.

The distance from the root to a leaf is the number of edges on the path from the root to that leaf. In the figure, the root nodes are blue, and the distance from the root to each leaf is shown. The sums of the root-to-leaf distances are 77 for T_LT\_L and 1010 for T_RT\_R.

Given a string of matched parentheses that represents exactly one rooted ordered tree, output the sum of the distances from the root to all leaf nodes.

Input

The input is one line containing a string of matched parentheses that represents one rooted ordered tree. The string contains no characters other than parentheses. Its length is at least 22 and at most 10710^7.

Output

Print one line containing the sum of the distances from the root of the tree to all leaf nodes.

Examples2

  1. Example 1

    Input
    ((()()())())
    
    Expected output
    7
    
  2. Example 2

    Input
    (()((()(()))()))
    
    Expected output
    10