cswork / OAOpen practice workspace

AmazonOA #358

Calculate Efficiency Score

Estimated HardJudge-ready

Topics (AI-labelled)Stacks & HeapsGreedy

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.

Input

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

Output a single integer, the answer.

Example 1

Input:

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

Output:

6

Example 2

Input:

(
1
)
-5

Output:

-5

Example 3

Input:

()
0


Output:

0

Example notes

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.

Editorial

Loading solution…