CF BUDDY
← Problems·

26B · Regular Bracket Sequence

1400 · greedy

Problem: 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: Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.

Output Format: Output the maximum possible length of a regular bracket sequence.

Sample Cases

Case 1

Input

(()))(

Output

4

Case 2

Input

((()())

Output

6

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback