#C9880. Reservoir Water Trapping with Leaks
Reservoir Water Trapping with Leaks
Reservoir Water Trapping with Leaks
You are tasked with computing the maximum amount of water that can be trapped in a reservoir after rainfall. The reservoir is modeled using an array of building heights along with a corresponding array that indicates leak positions. Water is trapped at a position if there is no leak there, and the amount of water that can be stored at that position is determined by the minimum of the highest building to its left and right minus its own height. Formally, if there is no leak at index (i), the water stored is
[
w_i = \max\Big(0, \min(\text{left_max}[i],; \text{right_max}[i]) - h_i\Big)]
where (h_i) is the height at position (i). If a leak is present (indicated by a 1 in the leaks array), that position cannot store any water. Note that if the number of positions (n) is less than or equal to 2, no water can be trapped.
Your program should read from standard input and output the result to standard output.
inputFormat
The input consists of multiple test cases. The first line contains an integer (T) representing the number of test cases. Each test case is described as follows:
- The first line contains an integer \(n\) — the number of positions in the reservoir.
- The second line contains \(n\) space-separated integers representing the heights at each position.
- The third line contains \(n\) space-separated integers (either 0 or 1) indicating the presence of a leak at each position (1 means a leak, 0 means no leak).
outputFormat
For each test case, output a single line containing one integer: the maximum water trapped in the reservoir.## sample
5
12
0 1 0 2 1 0 1 3 2 1 2 1
0 0 0 0 0 0 0 0 0 0 0 0
6
3 0 1 3 0 5
0 0 0 0 0 0
5
3 0 1 3 2
1 1 1 1 1
6
3 0 1 3 0 5
0 1 0 0 0 0
3
3 4 2
0 0 0
6
8
0
3
0
</p>