#P10115. [LMXOI Round 1] Placer
[LMXOI Round 1] Placer
Background
LMX has recently become obsessed with bracket sequences, and she especially likes valid bracket sequences.
To test HQZ’s sincerity, LMX made a problem to challenge HQZ.
Problem Description
LMX gives a bracket sequence of length , and a sequence of length .
Define $w(l,r)= \begin{cases} a_r-a_l, & S_{l..r} \text{ is a valid bracket sequence}\\ \ 0 & \text{otherwise} \end{cases}$
You can split the sequence into several non-empty subsegments. Define the beauty of the whole sequence as the sum of over all segments.
Find the maximum possible beauty.
Input Format
The first line contains an integer .
The second line contains a string representing the bracket sequence.
The third line contains the sequence .
Output Format
The first line contains an integer, representing the maximum beauty.
5
()(()
1 3 2 3 5
4
10
()((())())
2 4 1 7 3 2 8 4 9 5
8
Hint
Sample Explanation #1
The original string can be divided into three intervals: . The contribution is .
| Subtask ID | Special Property | Score | |
|---|---|---|---|
| Subtask #1 | None | ||
| Subtask #2 | |||
| Subtask #3 | The bracket sequence is | ||
| Subtask #4 | None |
For of the testdata, .
Translated by ChatGPT 5