#P1018. Problem 2. Milk Exchange

Problem 2. Milk Exchange

当前没有测试数据。

一、Problem Name

Milk Exchange

二、Problem Description

Farmer John's NN (1N21051\leq N\leq2\cdot10^5) cows are lined up in a circle such that for each ii in 1,2,,N11,2,\cdots,N - 1, the cow to the right of cow ii is cow i+1i + 1, and the cow to the right of cow NN is cow 11. The iith cow has a bucket with integer capacity aia_i (1ai1091\leq a_i\leq10^9) liters. All buckets are initially full.

Every minute, the cows exchange milk according to a string s1s2sNs_1s_2\cdots s_N consisting solely of the characters 'L' and 'R'. if the iith cow has at least 11 liter of milk, she will pass 11 liter of milk to the cow to her left if si=s_i = 'L', or to the right if si=s_i = '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 aia_i, then the excess milk will be lost.

FJ wants to know: after MM minutes (1M1091\leq M\leq10^9), what is the total amount of milk left among all cows?

三、Input Format (input arrives from the terminal / stdin)

  1. The first line contains NN and MM.
  2. The second line contains a string s1s2sNs_1s_2\cdots s_N consisting solely of the characters 'L' or 'R', denoting the direction each cow will pass their milk in.
  3. The third line contains integers a1,a2,,aNa_1,a_2,\cdots,a_N, the capacities of each bucket.

四、Output Format (print output to the terminal / stdout)

Output an integer, the sum of milk among all cows after MM minutes.

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++).

五、Sample Input and Output

Sample Input 1

3 1
RRL
1 1 1

Sample Output 1

2

Explanation: Cows 22 and 33 pass each other one liter of milk, so their milk is preserved. When cow 11 passes their milk to cow 22, cow 22's bucket overflows, and one liter of milk is lost after one minute.

Sample Input 2

5 20
LLLLL
3 3 2 3 3

Sample Output 2

14

Explanation: Each cow is passing a liter of milk to the cow on the left and gaining a liter of milk from the cow on the right, so all of the milk is preserved regardless of how much time passes.

Sample Input 3

9 5
RRRLRRLLR
5 8 4 9 3 4 9 5 4

Sample Output 3

38

Explanation: Initially, there are a total of 51 liters of milk. After 5 minutes, cows 33, 66, and 77 will lose 5, 3, and 5 liters of milk respectively. Therefore, a total of 38 liters of milk remain.

六、Scoring Rules

  1. Inputs 4 - 8: N,M1000N,M\leq1000.
  2. Inputs 9 - 16: No additional constraints.

Problem credits: Chongtian Ma, Alex Liang.