Milk Exchange

시간 제한2초메모리 제한1024 MB

요약
소들이 원형으로 배열된 상태에서 매 분마다 이웃에게 우유 1리터를 넘기고 넘친 양은 버려질 때 M분 뒤 남은 우유의 총합을 구한다.
난이도

어려움10점 중 8점

유형
시뮬레이션, 구현, 그리디, 수학
정답자
아직 제출이 없습니다

문제

Farmer John's NN (1≤N≤2⋅105)(1 \leq N \leq 2 \cdot 10^5) cows are lined up in a circle such that for each ii in 1,2,…,N−11,2,\dots,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 a_ia\_i (1≤a_i≤109)(1 \leq a\_i \leq 10^9) liters. All buckets are initially full.

Every minute, the cows exchange milk according to a string s_1s_2…s_Ns\_1s\_2\dots s\_N consisting solely of the characters ‘L’\text{‘L’} and ‘R’\text{‘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 s_i=‘L’s\_i=\text{‘L’}, or to the right if s_i=‘R’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_ia\_i, then the excess milk will be lost.

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

입력

The first line contains NN and MM.

The second line contains a string s_1s_2…s_Ns\_1s\_2\dots s\_N consisting solely of the characters ‘L’\text{‘L’} or ‘R’\text{‘R’}, denoting the direction each cow will pass their milk in.

The third line contains integers a_1,a_2,…,a_Na\_1, a\_2, \dots, a\_N, the capacities of each bucket.

출력

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

예제3

  1. 예제 1

    입력
    3 1
    RRL
    1 1 1
    
    예상 출력
    2
    
  2. 예제 2

    입력
    5 20
    LLLLL
    3 3 2 3 3
    
    예상 출력
    14
    
  3. 예제 3

    입력
    9 5
    RRRLRRLLR
    5 8 4 9 3 4 9 5 4
    
    예상 출력
    38