632B.md (1915B)
1 Source: https://codeforces.com/problemset/problem/632/B 2 3 B. Alice, Bob, Two Teams 4 5 time limit per test 6 7 1.5 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are *n* pieces, and the *i*-th piece has a strength *p**i*. 22 23 The way to split up game pieces is split into several steps: 24 25 1. First, Alice will split the pieces into two different groups *A* and *B*. This can be seen as writing the assignment of teams of a piece in an *n* character string, where each character is *A* or *B*. 26 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change *A* to *B* and *B* to *A*). He can do this step at most once. 27 3. Alice will get all the pieces marked *A* and Bob will get all the pieces marked *B*. 28 29 The strength of a player is then the sum of strengths of the pieces in the group. 30 31 Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. 32 33 Input 34 35 The first line contains integer *n* (1 ≤ *n* ≤ 5·105) — the number of game pieces. 36 37 The second line contains *n* integers *p**i* (1 ≤ *p**i* ≤ 109) — the strength of the *i*-th piece. 38 39 The third line contains *n* characters *A* or *B* — the assignment of teams after the first step (after Alice's step). 40 41 Output 42 43 Print the only integer *a* — the maximum strength Bob can achieve. 44 45 Examples 46 47 Input 48 49 ``` 50 5 51 1 2 3 4 5 52 ABABA 53 ``` 54 55 Output 56 57 ``` 58 11 59 ``` 60 61 Input 62 63 ``` 64 5 65 1 2 3 4 5 66 AAAAA 67 ``` 68 69 Output 70 71 ``` 72 15 73 ``` 74 75 Input 76 77 ``` 78 1 79 1 80 B 81 ``` 82 83 Output 84 85 ``` 86 1 87 ``` 88 89 Note 90 91 In the first sample Bob should flip the suffix of length one. 92 93 In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. 94 95 In the third sample Bob should do nothing.