#P9805. [POI 2022/2023 R1] ply
[POI 2022/2023 R1] ply
Background
This problem is translated from POI2022~2023R1 ply.
Problem Description
Define a "valid bracket sequence" and its depth as follows:
- The empty string is a valid bracket sequence, with depth .
- If is a valid bracket sequence with depth , then is also a valid bracket sequence with depth .
- If and are both valid bracket sequences with depths and , then is also a valid bracket sequence, with depth .
Define flipping a character as:
- If the current character is
(, change it to). - If the current character is
), change it to(.
You need to flip some characters in so that the depth does not exceed . Find the minimum number of operations.
Input Format
The first line contains two integers and , representing and the required maximum depth after modification.
The second line contains a string , representing the original bracket sequence.
Output Format
Output the minimum number of modifications.
8 2
(()(()))
2
Hint
For the sample, you can modify it to (()()()), whose depth is .
The subtasks are as follows:
| Subtask ID | Special Property | Score |
|---|---|---|
| and | ||
Note: is the depth of the input bracket sequence.
In this problem, subtask is the sample.
Translated by ChatGPT 5