(define (puddle land)
(let loop ((volume 0) (lmax 0) (rmax 0) (left 0)
(right (- (vector-length land) 1)))
(cond ((<= right left) volume)
((< lmax (vector-ref land left))
(loop volume (vector-ref land left) rmax left right))
((< rmax (vector-ref land right))
(loop volume lmax (vector-ref land right) left right))
((< rmax lmax)
(loop (+ volume rmax (- (vector-ref land right)))
lmax rmax left (- right 1)))
(else (loop (+ volume lmax (- (vector-ref land left)))
lmax rmax (+ left 1) right)))))
(display (puddle '#(2 5 1 2 3 4 7 7 6))) (newline)
(display (puddle '#(2 5 1 3 1 2 1 7 7 6))) (newline)
(display (puddle '#(2 7 2 7 4 7 1 7 3 7))) (newline)
(display (puddle '#(6 7 7 4 3 2 1 5 2))) (newline)