Determine the amount to save each year to cover expenses Imagine owning a big house that require a lot of maintenance. To know what you need to spend on maintenance in advance you take in a professional that creates a maintenance plan covering 50 years in the future. The plan contains how much you need to spend each year to cover expected maintenance. Some years might have zero expenses, and some might have very big ones.
To avoid a chock when the big ones come, you want to save an amount of money each year to cover future maintenance spending.
The question is, how much to save each year?
Some additional requirements:

*

*The amount can very from year to year, but the amount each year should differ as little as possible between years.

*After the 50 years is over, the amount in the maintenance saving account should be as close to zero as possible.

*The maintenance saving account could already have a starting amount of money.

Me not working in math I tried to think of some different methods to arrive at the best solution.
One would be to start with zero in saving each year and then traverse each year deducting the expenses for that year if any. Once the maintenance account got below zero, (say it went to -1000), then you divide the 1000 on the years prior and add that amount to the saving plan. Then you start again traversing the years from the beginning but with the updated savings plan. Do this over and over until you never reach below zero.
However as you are getting close to the 50 year you will be splitting larger expenses on all years prior, so that might create a larger amount each year in the first years then declining, which would violate the requirement to keep the sum as static as possible.
Is there a better way to think about this?
 A: You seem to be assuming no interest on accumulative savings and no inflation. That makes the calculation easier. On your third point; if the account starts with a lump sum, then it can be included as a negative amount in the cumulative spending calculations below, at least for the initial pass.
Suppose you save $X_1,X_2,\ldots,X_n$ each year and spend $Y_1,Y_2,\ldots,Y_n$ each year.  Let's call cumulative saving to year $k$: $C_k=\sum\limits_{i=1}^k X_i$; and cumulative spending: $S_k=\sum\limits_{i=1}^k Y_i$ (with any initial lump sum in the account deducted from all the $S_k$).  You have the constraints

*

*total saving at the end equals total spending $C_n = S_n$ and

*at any stage cumulative spending is at least cumulative spending $C_k \ge S_k$ for $1 \le k \le n$
Now consider average spending up to year $k$: $A_k=\frac1k S_k$.  Find which $k$ maximises this (if there is more than one take the greatest) and call it $m$.  Up to year $m$ you could save a constant $X_i=A_m$ each year for $1 \le k \le m$ and you would satisfy the second condition, leaving the account empty at year $m$.
If $m=n$ then you are done and you have a completely flat profile.  If not, then a flat profile over the whole period would not lead to $C_n=S_m$, so start again with $n-m$ years (instead of $n$) and $X_{m+1},X_{m+2},\ldots,X_n$ and $Y_{m+1},Y_{m+2},\ldots,Y_n$ and repeat.  Since you are reducing the number of years each time, this is a finite method which must terminate.
You will end up with a pattern for $X_1,X_2,\ldots,X_n$ which is flat up to each point at which $C_m=S_m$ so you cannot do better in the early years, and then reduces to another flat profile and so on until you end with $C_n=S_n$.
