Limits 3.5s, 512 MB

You have vacation for the next NN days. Each day you will either create a problem or you will hold a contest with exactly one problem. Formally speaking, your action for the next NN days can be described by a string of length NN containing characters ‘P’ and ‘C’. If the ithi^{th} character is ‘P’ then you will be creating a problem on ithi^{th} day, if ithi^{th} character is ‘C’ then you will hold a contest on that day with a problem that is created before ithi^{th} day and unused till then. Each problem has an interesting factor and each contest has an importance factor. If you put a problem with interesting factor xx to a contest of importance factor yy, then you get x×yx \times y satisfaction.

You need to put problems to contests in such a way that the sum of your satisfaction is maximized. Also the interesting factor of the problems are non increasing as your are not getting any sharper day by day. Input will be generated in a way that, for each contest you will have at least one problem available and as you love contests, you will not miss the chance to be the problem-setter in any contest.

Input

First line contains number of test cases T(1T10000)T(1\le T\le 10000).

Each testcase starts with an integer N(1N106)N(1\le N\le 10^6).

The next line contains a string A(A[1]A[N])A(A[1]…A[N]) containing NN characters, each character is either ‘P’ or ‘C’.

The next line contains NN space separated integers V[1],V[2],.,V[N]V[1],V[2],….,V[N], if A[i]=PA[i] = ‘P’ then V[i]V[i] denotes interesting factor of the problem that you create on ithi^{th} day, else V[i]V[i] denotes importance factor of the contest on ithi^{th} day.

For all 1iN1\le i\le N, 1V[i]1061\le V[i]\le 10^6

If i<ji \lt j and A[i]=A[j]=PA[i] = A[j] = ‘P’ then V[i]V[j]V[i] \ge V[j]

It's guaranteed that the sum of NN over all test cases does not exceed 10610^6.

Output

For each case, output a single integer, which denotes the maximum satisfaction you can get if you optimally place the problems in the contests.

Sample

InputOutput
2
5
PPCPC
9 8 9 6 6
5
PPCPC
9 8 1 7 9
129
89

For the first testcase,
Answer is 99+68=1299*9 + 6*8 = 129. On the 1st1^{st} contest (day 33) you will put the problem created on day 11 and on the 2nd2^{nd} contest (day 55) you will assign the problem created on day 22.

For the second testcase,
Answer is 81+99=898*1 + 9*9 = 89. On the 1st1^{st} contest (day 33) you will put the problem created on day 22 and on the 2nd2^{nd} contest (day 55) you will assign the problem created on day 11.

Submit

Login to submit.

Statistics

65% Solution Ratio
IbnaHamzaEarliest, 9M ago
n3wb13_223Fastest, 0.1s
Sarwar82Lightest, 7.6 MB
tahmidarefinShortest, 695B
Toph uses cookies. By continuing you agree to our Cookie Policy.