#P6827. 「EZEC-4」括号

    ID: 5734 远端评测题 1000~2200ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dpO2优化状态压缩,状压块状链表,块状数组,分块

「EZEC-4」括号

Description

You are given a sequence consisting of round brackets and nn bracket strings. You need to match subsequences (not necessarily contiguous substrings) within the sequence.

Each bracket in the main sequence can be matched at most once.

One possible way to match a non-contiguous substring is to match ))( inside )()(.

Each bracket string has a value vv, meaning the value gained for each matched bracket of this string.

Each bracket string can be matched multiple times. You may stop matching at any time, but you cannot start matching another bracket string before finishing the current one.

Note that if you skip some brackets in the main sequence to match later ones, you cannot go back to continue matching earlier positions.

Find the maximum total value obtainable.

Input Format

The first line contains an integer nn, the number of bracket strings.

The second line contains a string kk, the given main sequence.

The next nn lines each contain a bracket string aa and a number vv. In these nn lines, the ii-th line gives ai,via_i, v_i, representing an existing bracket string and the value obtained for matching one bracket in this bracket string.

Output Format

Output one number rr, the maximum value that can be obtained.

3 
((()())(()
(() 4
() 2
()()() 5
32
3
())()))()
()) 9
()() 8
())) 7
73

Hint

[Warm Reminder]

To eliminate wrong solutions, the Constraints have been increased. Please pay attention to the impact of constant factors on your program.

[Constraints]

This problem uses bundled testdata.

For 100%100\% of the testdata, $1 \le n \le 500, 1 \le len(k) \le 10^4, 1 \le len(a) \le 300, 0 \le v \le 10^3$.

Subtask n n \le len(k) len(k) \le len(a) len(a) \le Time Score Special Property
11 1010 77 1s1\text s 55 Guaranteed that in the optimal solution, each aa is used at most once.
22 5050 88 Random testdata; if you AC Subtask 3, this subtask is not counted in the total score.
33 10001000 1010 Guaranteed that in the optimal solution, each aa is used at most once.
44 100100 10410^4 1515 None.
55 500500 1010
66 300300 2.2s2.2\text s 6060

[Sample 11 Explanation]

The best plan is to first match (() and then match ()()() (note that you can match at most ()()), so the answer is 4×3+5×4=324 \times 3 + 5 \times 4 = 32.

One feasible matching method is the parts enclosed in square brackets: (【(()()】)(【()】.

If you first match ((), then match (), and finally match ((), the value is 4×3+2×2+4×3=284 \times 3 + 2 \times 2 + 4 \times 3 = 28, which is not optimal.

Note that we cannot first match ((), then match one bracket of ()()(), and finally match ((), because although we may stop matching at any time, we cannot start matching another string before finishing the current one.

[Sample 22 Explanation]

The best plan is to first match ()), then match ())), and finally match ()) (note that you can match at most ()), so the answer is 9×3+7×4+9×2=739 \times 3 + 7 \times 4 + 9 \times 2 = 73.

Translated by ChatGPT 5