3727.Regular Bracket Sequence

Time Limit: 1s Memory Limit: 256MB

A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.

One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?

Input Format(From the terminal/stdin)

Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.

Output Format(To the terminal/stdout)

Output the maximum possible length of a regular bracket sequence.

Sample Input 1

Copy
(()))(
      \n

Sample Output 1

Copy
4
 \n

Sample Input 2

Copy
((()())
       \n

Sample Output 2

Copy
6
 \n

Submit

请先 登录

© 2025 FAQs