10450: USACO 2024 February Contest, Bronze Problem 2. Milk Exchange
Description
Every minute, the cows exchange milk according to a string $s_1s_2\dots s_N$ consisting solely of the characters $\text{‘L’}$ and $\text{‘R’}$. if the $i$th cow has at least $1$ liter of milk, she will pass $1$ liter of milk to the cow to her left if $s_i=\text{‘L’}$, or to the right if $s_i=\text{‘R’}$. All exchanges happen simultaneously (i.e., if a cow has a full bucket but gives away a liter of milk but also receives a liter, her milk is preserved). If a cow's total milk ever ends up exceeding $a_i$, then the excess milk will be lost.
FJ wants to know: after $M$ minutes $(1 \leq M \leq 10^9$), what is the total amount of milk left among all cows?
Input
The second line contains a string $s_1s_2\dots s_N$ consisting solely of the characters $\text{‘L’}$ or $\text{‘R’}$, denoting the direction each cow will pass their milk in.
The third line contains integers $a_1, a_2, \dots, a_N$, the capacities of each bucket.
Output
Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).
Constraints
- Inputs 4-8: $N,M \le 1000$
- Inputs 9-16: No additional constraints.
Sample 1 Input
3 1
RRL
1 1 1
Sample 1 Output
2
Sample 2 Input
5 20
LLLLL
3 3 2 3 3
Sample 2 Output
14
Sample 3 Input
9 5
RRRLRRLLR
5 8 4 9 3 4 9 5 4
Sample 3 Output
38