cswork / OA打开练习工作台

AmazonOA #358

Calculate Efficiency Score

估计困难可在线判题

考点(AI 标注)单调栈与堆贪心调度

Keeping the order of the original bracket string, insert unused brackets from the kit at any positions so that the final string is balanced, and maximize the total score of the brackets used. The original string itself scores 0. The original statement guarantees that a feasible completion exists, and brackets that are required must be chosen even if their scores are negative.

输入格式

The first line contains s, the second line contains m, the third line contains kit, and the fourth line contains m scores; when m=0 the last two lines are empty. The strict original bounds are 1≤|s|≤199999 and 0≤m≤199999; kit has length m and contains only left and right brackets, and scores are in −10^9..10^9.

输出格式

Output a single integer, the answer.

样例 1

输入:

)((
6
)(()))
3 4 2 -4 -1 -3

输出:

6

样例 2

输入:

(
1
)
-5

输出:

-5

样例 3

输入:

()
0


输出:

0

样例说明

The original TODO example is filled in as 6; the second example can only add a right bracket worth negative 5, so the answer is −5; the third example gives 0.