This is a weird optimization problem I recently came across which I cannot solve.
Suppose we start off with an empty container of M&Ms. We take and add M&Ms to the container every year for $n$ years. The number of M&Ms I take out each year, $B$, is always the same (think of it as a parameter). The number added in year $i$, $A(i)$, is known (think of it as data).
If $A(i) - B < 0$, I have an M&M "bank" I can go to cover my losses and so I can keep operating even with a deficit. We let $D(0)$ represent the initial amount in the bank and $D(i)$ denote the amount in the bank in year $i$, where we calculate $D(i)$ as: $D(i) = D(i-1) + A(i) - B$, if $A(i) - B <= 0$; $D(i) = \min(D(0), D(i-1) + A (i) - B)$, if $A(i) - B > 0$ (in the latter case, I throw away any extra M&Ms left over after I cover my debt to the bank).
The problem is: "Find the maximum value of $B$ such that $D(n) = D(0)$" (i.e., what is the maximum number of M&Ms I can take out every year, so I have no deficit with the bank at the end of the $n$ years?).