CF1685C Bring Balance

Description

Alina has a bracket sequence $ s $ of length $ 2n $ , consisting of $ n $ opening brackets '(' and $ n $ closing brackets ')'. As she likes balance, she wants to turn this bracket sequence into a balanced bracket sequence. In one operation, she can reverse any substring of $ s $ . What's the smallest number of operations that she needs to turn $ s $ into a balanced bracket sequence? It can be shown that it's always possible in at most $ n $ operations. As a reminder, a sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters + and 1. For example, sequences (())(), (), and (()(())) are balanced, while )(, ((), and (()))( are not.

Input Format

N/A

Output Format

N/A

Explanation/Hint

In the first test case, the string is already balanced. In the second test case, the string will be transformed as follows: ())((()))( $ \to $ ()()(()))( $ \to $ ()()(())(), where the last string is balanced. In the third test case, the string will be transformed to ((()))((())), which is balanced.