Seonggwan has one task scheduled on each of the next N days. He is worried that he does not have enough energy for all of them.
On the first day Seonggwan starts with E energy. He can spend as much energy as he wants on a day. At the end of a day he recovers R energy, but his total energy never goes above E. That is, if a energy is left at the end of a day, he starts the next day with min(E,a+R) energy.
The more energy he spends on a day, the better that day's task goes. The tasks differ in importance, so Seonggwan wants to spend more energy on the more important ones and use his energy as efficiently as possible. The task on day i has importance ci and he spends ei energy that day. Find the distribution of energy that maximizes c1e1+c2e2+⋯+cnen.