#P8293. [省选联考 2022] 序列变换
[省选联考 2022] 序列变换
Problem Description
You have a valid bracket sequence of length in your hand. Each left parenthesis in has a weight.
In your view, different bracket sequences bring different visual aesthetics. Therefore, you especially like bracket sequences with certain structures, and dislike bracket sequences with some other structures. You want to apply some transformations to to eliminate the structures you do not like.
Specifically, you like structures of the form (where and are both valid bracket sequences; the same below), and you dislike structures of the form . You have two operations to change the positions among brackets.
These two operations are:
- Operation 1: In a string of the form , swap the two brackets between and , transforming it into (where and are arbitrary strings and may be empty, but they are not necessarily valid bracket sequences respectively; the same below). Its cost is times the weight of the first left parenthesis in plus times the weight of the first left parenthesis in , where .
- Operation 2: In a string of the form , swap and , transforming it into . This operation has no cost.
Note: When swapping, all weights of left parentheses move together with their corresponding parentheses.
Now you want to know: what is the minimum cost to transform into a bracket sequence that does not contain the structure you dislike?
Input Format
The first line contains three integers .
The second line contains a valid bracket sequence of length , representing .
The third line contains positive integers, where the -th one denotes the weight of the -th left parenthesis from the left.
Output Format
Output one integer in a single line, representing the minimum cost to transform into a bracket sequence that does not contain the structure you dislike.
2 0 1
()()
1 3
1
2 1 0
()()
1 3
1
见附件中的 bracket/bracket3.in
见附件中的 bracket/bracket3.ans
Hint
[Sample Explanation #1]
The optimal plan is to first use Operation 2 to swap the two pairs of parentheses, and then use Operation 1 (at this time , , , and are all empty strings) to swap the two middle brackets. The cost is the weight of the bracket to the left of , which is . Finally, we obtain the bracket sequence , which does not contain the structure you dislike.
[Sample Explanation #2]
The optimal plan is to use Operation 1 directly, because the way to compute the cost is different this time: only the weight of the bracket to the left of is counted as the cost.
[Constraints]
It is guaranteed that and .
It is guaranteed that all weights are within .
| Test Point ID | Special Constraints |
|---|---|
| All weights are equal | |
| , | |
| No special constraints |
[Hint]
A string is called a valid bracket sequence if and only if consists only of an equal number of characters and , and for every prefix of , the number of is at least the number of . In particular, the empty string is also a valid bracket sequence.
Translated by ChatGPT 5