You have vacation for the next 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 days can be described by a string of length containing characters ‘P’ and ‘C’. If the character is ‘P’ then you will be creating a problem on day, if character is ‘C’ then you will hold a contest on that day with a problem that is created before 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 to a contest of importance factor , then you get 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.
First line contains number of test cases .
Each testcase starts with an integer .
The next line contains a string containing characters, each character is either ‘P’ or ‘C’.
The next line contains space separated integers , if then denotes interesting factor of the problem that you create on day, else denotes importance factor of the contest on day.
For all ,
If and then
It's guaranteed that the sum of over all test cases does not exceed .
For each case, output a single integer, which denotes the maximum satisfaction you can get if you optimally place the problems in the contests.
Input | Output |
---|---|
2 5 PPCPC 9 8 9 6 6 5 PPCPC 9 8 1 7 9 | 129 89 |
For the first testcase,
Answer is . On the contest (day ) you will put the problem created on day and on the contest (day ) you will assign the problem created on day .
For the second testcase,
Answer is . On the contest (day ) you will put the problem created on day and on the contest (day ) you will assign the problem created on day .