Water at i = max(0, min(leftMax,rightMax)-height[i]). Two pointers / pref-max variant.
Input
12 0 1 0 2 1 0 1 3 2 1 2 1
Output
6
6 4 2 0 3 2 5
9