Brewing potion 4 - MarisaOJ: Marisa Online Judge

Brewing potion 4

Time limit: 1000 ms
Memory limit: 256 MB
Today Marisa wants to make a potion bottle out of $m$ type of mushrooms. There are $n$ mushrooms placed in a row. The $i^{th}$ mushroom is of the type $A_i$. But there are some restrictions: Marisa can't put more than $B_i$ mushrooms of type $i$ in the same bottle, because that would make the potion unstable and explode. She will choose a consecutive segment of mushrooms. Help her find the maximum number of mushrooms she can put in her potion. ### Input - The first line contains 2 integers $n, m$. - The second line contains $n$ integers $A_i$. - The third line contains $m$ integers $B$. ### Output - The maximum number of mushrooms Marisa can use. ### Constraints - $1 \le n, m \le 10^5$. - $1 \le A_i \le m$. - $0 \le B_i \le 10^5$. ### Example Input: ``` 5 3 1 2 3 2 1 1 2 1 ``` Output: ``` 4 ```