thd (_,_,x) = x
waterInCity = thd . waterInCity' (0,[],0)
waterInCity' t [] = t
waterInCity' (highest
,buildings
,water
) (x:
[]) = (highest
,[],water
+ sum(map ((min highest x
)-) buildings
)) waterInCity' (highest,buildings,water) (x:xs) = if x >= highest
then waterInCity' (x
,[],water
+ sum(map (highest
-) buildings
)) xs
else waterInCity' (highest,x:buildings,water) xs
main = do
print $ waterInCity [] == 0
print $ waterInCity [1] == 0
print $ waterInCity [1,1] == 0
print $ waterInCity [1,0] == 0
print $ waterInCity [0,1] == 0
print $ waterInCity [1,0,1] == 1
print $ waterInCity [2,0,1,3] == 3
print $ waterInCity [6,3,4,7,4,3,7,7] == 3 + 2 + 3 + 4
print $ waterInCity [2,5,1,2,3,4,7,7,6] == 10
print $ waterInCity [1,5,4,3,2,1]
dGhkIChfLF8seCkgPSB4Cgp3YXRlckluQ2l0eSA6OiBbSW50XSAtPiBJbnQKd2F0ZXJJbkNpdHkgPSB0aGQgLiB3YXRlckluQ2l0eScgKDAsW10sMCkgCgp3YXRlckluQ2l0eScgOjogKEludCxbSW50XSxJbnQpIC0+IFtJbnRdIC0+IChJbnQsW0ludF0sSW50KQp3YXRlckluQ2l0eScgdCBbXSA9IHQKd2F0ZXJJbkNpdHknIChoaWdoZXN0LGJ1aWxkaW5ncyx3YXRlcikgKHg6W10pID0gKGhpZ2hlc3QsW10sd2F0ZXIgKyBzdW0obWFwICgobWluIGhpZ2hlc3QgeCktKSBidWlsZGluZ3MpKQp3YXRlckluQ2l0eScgKGhpZ2hlc3QsYnVpbGRpbmdzLHdhdGVyKSAoeDp4cykgPSBpZiB4ID49IGhpZ2hlc3QgCgl0aGVuIHdhdGVySW5DaXR5JyAoeCxbXSx3YXRlciArIHN1bShtYXAgKGhpZ2hlc3QtKSBidWlsZGluZ3MpKSB4cwoJZWxzZSB3YXRlckluQ2l0eScgKGhpZ2hlc3QseDpidWlsZGluZ3Msd2F0ZXIpIHhzCgptYWluID0gZG8gCglwcmludCAkIHdhdGVySW5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