Repovive
  • Dashboard
  • Ranking
  • Submissions
  • Discussion
  1. Contests
  2. Premier Round 9
  3. 27F

My submissions

/
/
/
/

Announcements

My submissions

/
/
/
/

Announcements

Solve
Author

27F. Cooling Sushi

3000 pts·3s·256 MB

Constraints

Input

Output

Samples

Input
t
2
Case 1
1
C
7
Case 2
3
RRC
6 6 5
Explanation
Case 1
Case 2
Output
Case 1
7
Case 2
5
Input
t
3
Case 1
4
RRRR
8 2 8 5
Case 2
4
CCCC
1 1 1 1
Case 3
3
RCC
1 1 1
Explanation
Case 1
Case 2
Case 3
Output
Case 1
3
Case 2
2
Case 3
0

There are nnn pieces of sushi on a table. Each piece is either raw or cooked, as described by a string sss: sis_isi​ is R for raw sushi and C for cooked sushi. Piece iii initially has a score of aia_iai​.

Alice and Bob take turns, with Alice going first. On each turn, the player eats any one remaining piece of sushi and adds its current score to their total. Then the score of every remaining cooked piece decreases by 111. The scores of raw pieces do not change.

The game ends when all pieces have been eaten. Alice wants to maximize her total score minus Bob's total score, while Bob wants to minimize this difference.

Find the final difference if both players play optimally.

A piece's score may become negative. It must still be eaten before the game ends, and eating it adds that negative score to the player's total. Neither player may skip a turn.

  • 1≤t≤1041 \le t \le 10^41≤t≤104
  • 1≤n≤2×1051 \le n \le 2\times 10^51≤n≤2×105
  • 1≤ai≤1091 \le a_i \le 10^91≤ai​≤109
  • sss is a string of length nnn consisting only of R and C.
  • The sum of nnn over all test cases is at most 2×1052\times 10^52×105.
ttt ns1s2…sna1a2…an}×t\left. \begin{array}{l} n \\ s_1s_2\ldots s_n \\ a_1 \quad a_2 \quad \ldots \quad a_n \end{array} \right\} \times tns1​s2​…sn​a1​a2​…an​​⎭⎬⎫​×t
answer}×t\left. \begin{array}{l} \mathrm{answer} \end{array} \right\} \times tanswer​}×t

Alice eats the only piece and gains 777 points. Bob gains 000, so the difference is 777.

Alice eats the cooked piece first and gains 555. Bob and Alice then each eat one raw piece worth 666, giving a difference of 555. If Alice starts with a raw piece instead, Bob can eat the other raw piece, leaving the cooked piece worth 333 for Alice; that choice gives only 333.

One optimal play takes pieces with scores 8,8,5,28,8,5,28,8,5,2, in that order. Alice gains 131313 and Bob gains 101010, so the difference is 333.

All remaining pieces always have the same score. The scores gained on successive turns are 1,0,−1,−21,0,-1,-21,0,−1,−2. Alice totals 000, Bob totals −2-2−2, and the difference is 222.

Alice can eat a cooked piece first. Bob then eats the raw piece for 111, leaving a cooked piece worth −1-1−1 for Alice, for a difference of −1-1−1. Instead, Alice can start with the raw piece: the two cooked pieces are then eaten for 000 and −1-1−1, giving a difference of 000. Thus optimal play gives 000.