#P12249. Lazy Juice Pouring
Lazy Juice Pouring
Lazy Juice Pouring
Little Keke bought delicious juice but is too lazy to pour it himself, so he asks Nailyon to pour the juice for him. There are \( m \) cups, where the \( i \)-th cup has a capacity of \( V_i \). There are \( n \) units of juice, and Nailyon will perform exactly \( n \) pouring operations (one unit per operation). In the \( j \)-th operation, Nailyon is only allowed to pour the unit of juice into cup \( a_j \) or cup \( a_j+1 \). Specifically,
- If both cups are not full, he may choose either one.
- If exactly one of them is not full, he must pour into that cup.
- If both cups are already full, then he happily drinks that unit of juice.
Moreover, the pouring operations are arranged from left to right, meaning that \( a_j \le a_{j+1} \) for all \( 1 \le j < n \). Given \( T \) test cases, determine the maximum number of juice units Nailyon can drink (i.e. the maximum number of pouring operations during which the juice is not poured into any cup because both eligible cups are full).
inputFormat
The first line contains an integer \( T \) representing the number of test cases. For each test case, the input is given in the following three lines:
- The first line contains two integers \( m \) and \( n \) — the number of cups and the total units of juice (i.e. the number of pouring operations), respectively.
- The second line contains \( m \) integers \( V_1, V_2, \ldots, V_m \) where \( V_i \) denotes the capacity of the \( i \)-th cup.
- The third line contains \( n \) integers \( a_1, a_2, \ldots, a_n \). In the \( j \)-th pouring, the juice can only be poured into cup \( a_j \) or cup \( a_j+1 \). It is guaranteed that \( a_j \le a_{j+1} \) for all \( 1 \le j < n \). Note that it is assumed that \( a_j+1 \) does not exceed \( m \).
outputFormat
For each test case, output a single integer — the maximum number of juice units that Nailyon can drink.
sample
1
2 3
1 1
1 1 1
1
</p>