#P8293. [省选联考 2022] 序列变换

    ID: 9378 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>贪心树形数据结构各省省选2022O2优化

[省选联考 2022] 序列变换

Problem Description

You have a valid bracket sequence ss of length 2n2 n in your hand. Each left parenthesis in ss 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 ss to eliminate the structures you do not like.

Specifically, you like structures of the form (A()B)\texttt{(A()B)} (where A\texttt{A} and B\texttt{B} are both valid bracket sequences; the same below), and you dislike structures of the form (A)(B)\texttt{(A)(B)}. You have two operations to change the positions among brackets.

These two operations are:

  • Operation 1: In a string of the form p(A)(B)q\texttt{p(A)(B)q}, swap the two brackets between A\texttt{A} and B\texttt{B}, transforming it into p(A()B)q\texttt{p(A()B)q} (where p\texttt{p} and q\texttt{q} are arbitrary strings and may be empty, but they are not necessarily valid bracket sequences respectively; the same below). Its cost is xx times the weight of the first left parenthesis in (A)\texttt{(A)} plus yy times the weight of the first left parenthesis in (B)\texttt{(B)}, where x,y{0,1}x, y \in \{0, 1\}.
  • Operation 2: In a string of the form pABq\texttt{pABq}, swap A\texttt{A} and B\texttt{B}, transforming it into pBAq\texttt{pBAq}. 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 ss into a bracket sequence that does not contain the structure you dislike?

Input Format

The first line contains three integers n,x,yn, x, y.

The second line contains a valid bracket sequence of length 2n2n, representing ss.

The third line contains nn positive integers, where the ii-th one denotes the weight of the ii-th left parenthesis from the left.

Output Format

Output one integer in a single line, representing the minimum cost to transform ss 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 A\texttt{A}, B\texttt{B}, p\texttt{p}, and q\texttt{q} are all empty strings) to swap the two middle brackets. The cost is the weight of the bracket to the left of B\texttt{B}, which is 11. Finally, we obtain the bracket sequence (())\texttt{(())}, 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 A\texttt{A} is counted as the cost.

[Constraints]

It is guaranteed that 2n4000002 \le n \le 400000 and 0x,y10 \le x, y \le 1.

It is guaranteed that all weights are within [1,107][1, {10}^7].

Test Point ID Special Constraints
131 \sim 3 n8n \leq 8
454 \sim 5 All weights are equal
686 \sim 8 n20n \leq 20
9129 \sim 12 x=0x = 0y=1y = 1
131613 \sim 16 n2000n \le 2000
172517 \sim 25 No special constraints

[Hint]

A string ss is called a valid bracket sequence if and only if ss consists only of an equal number of characters (\texttt{(} and )\texttt{)}, and for every prefix of ss, the number of (\texttt{(} is at least the number of )\texttt{)}. In particular, the empty string is also a valid bracket sequence.

Translated by ChatGPT 5