| 2025 USP Try-outs |
|---|
| Finished |
In the heart of the Forbidden City, the Emperor has ordered the performance of the great "Celestial Dance" to ensure the harmony of the empire. A sequence of dancers, representing the primordial forces, is positioned in a line. Yang dancers (represented by '(') and Yin dancers (represented by ')') form a sequence.
For the grand finale of the dance, each Yang dancer must be paired with a Yin dancer who is positioned further down the line. The beauty of this ancestral dance lies in its complexity: the paths of the pairs can cross, symbolizing the intertwined threads of fate. Therefore, multiple choreographies are possible, with different ways to pair Yin and Yang dancers. It is guaranteed that there always exists a valid choreography.
However, the imperial astrologers have warned that certain pairs are "incompatible". An incompatible pair is a specific Yang dancer and Yin dancer who, if paired together, make it impossible for all the other dancers to find a partner, thus breaking the cosmic harmony of the ritual.
For example, in the formation (()), any pair that is formed (such as the 1st with the 3rd, or the 1st with the 4th) always allows the remaining dancers to form a valid pair. Therefore, for (()), there are no incompatible pairs. However, in the formation ()(), if we try to pair the 1st dancer (Yang) with the 4th dancer (Yin), the remaining dancers are the 2nd (Yin) and the 3rd (Yang). Since the remaining Yin dancer comes before the Yang dancer, they cannot form a pair. Thus, the pair (1, 4) is incompatible.
Your task, as the Imperial Master Choreographer, is to analyze the initial formation of the dancers and determine the total number of incompatible pairs, so they can be avoided in the ceremony.
The first line contains a single integer $$$N$$$ ($$$1 \leq N \leq 10^6$$$), the number of dancers in the sequence.
The second line contains a sequence of $$$N$$$ characters, representing the dancers. It is guaranteed that there always exists a valid choreography.
Print a single integer: the number of incompatible pairs in the given sequence.
4(())
0
4()()
1
8(()())()
3
| Name |
|---|


