Help greatly appreciated! edge case fails for milk measurement

Why does the following code fail for Milk Measurement? (tc 4, 6, 7)
Problem: http://www.usaco.org/index.php?page=viewproblem2&cpid=763
Solution: USACO 2017 Dec Contest, Silver #2 - Replit