#arc175b. B - Parenthesis Arrangement
B - Parenthesis Arrangement
Score: points
问题陈述
给定一个长度为 的字符串 ,由字符 (
和 )
组成。设 表示 中从左数第 个字符。
你可以执行以下两种操作,每种操作可以执行零次或多次,且执行顺序任意:
-
选择一对整数 ,使得 。交换 和 。此操作的成本为 。
-
选择一个整数 ,使得 。将 替换为
(
或)
。此操作的成本为 。
你的目标是使 成为一个正确的括号序列。找到实现此目标所需的最小总成本。可以证明,目标总是可以通过有限次操作实现。
什么是正确的括号序列?满足以下任一条件的字符串即为正确的括号序列:
- 它是一个空字符串。
- 它是由按照
(
, ,)
顺序连接而成的,其中 是一个正确的括号序列。 - 它是由按照 和 顺序连接而成的,其中 和 都是正确的括号序列。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
You are given a string of length consisting of the characters (
and )
. Let denote the -th character from the left of .
You can perform the following two types of operations zero or more times in any order:
-
Choose a pair of integers such that . Swap and . The cost of this operation is .
-
Choose an integer such that . Replace with
(
or)
. The cost of this operation is .
Your goal is to make a correct parenthesis sequence. Find the minimum total cost required to achieve this goal. It can be proved that the goal can always be achieved with a finite number of operations.
What is a correct parenthesis sequence? A correct parenthesis sequence is a string that satisfies any of the following conditions:
- It is an empty string.
- It is formed by concatenating
(
, ,)
in this order where is a correct parenthesis sequence. - It is formed by concatenating and in this order where and are correct parenthesis sequences.
Constraints
- All input values are integers.
- is a string of length consisting of the characters
(
and)
.
Input
The Input is given from Standard Input in the following format:
Output
Print the answer in a single line.
Sample Input 1
3 3 2
)))(()
Sample Output 1
5
Here is one way to operate:
- Swap and . becomes
))()()
. The cost is . - Replace with
(
. becomes()()()
, which is a correct parentheses sequence. The cost is .
In this case, we have made a correct bracket sequence for a total cost of . There is no way to make a correct bracket sequence for less than .
Sample Input 2
1 175 1000000000
()
Sample Output 2
0
The given is already a correct bracket sequence, so no operation is needed.
Sample Input 3
7 2622 26092458
))()((((()()((
Sample Output 3
52187538