문제
Farmer John's cows are lined up in a circle such that for each in , the cow to the right of cow is cow , and the cow to the right of cow is cow . The th cow has a bucket with integer capacity liters. All buckets are initially full.
Every minute, the cows exchange milk according to a string consisting solely of the characters and . if the th cow has at least liter of milk, she will pass liter of milk to the cow to her left if , or to the right if . 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 , then the excess milk will be lost.
FJ wants to know: after minutes ), what is the total amount of milk left among all cows?
입력
The first line contains and .
The second line contains a string consisting solely of the characters or , denoting the direction each cow will pass their milk in.
The third line contains integers , the capacities of each bucket.
출력
Output an integer, the sum of milk among all cows after 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++).
예제 입력 1
3 1
RRL
1 1 1
예제 출력 1
2
예제 입력 2
5 20
LLLLL
3 3 2 3 3
예제 출력 2
14
예제 입력 3
9 5
RRRLRRLLR
5 8 4 9 3 4 9 5 4
예제 출력 3
38
점수
Inputs 4-8: Inputs 9-16: No additional constraints.
코드를 제출하려면 로그인이 필요합니다.
로그인