Veshy has been playing too much 8-ball pool recently, so to cure his addiction, he has turned to collecting pool cue balls instead. While looking into the market for cue balls, he realizes that there is a lot of money to be made. Each month, he can buy and sell a maximum of and cue balls at a price of and respectively for each month . Cue balls, being very valuable, also cost maintenance (per held cue ball) to upkeep per month. Being Veshy's best friend, you wish to help him maximize his profits over the course of months. Since Veshy is looking at very long term investments, help him by writing a program!
For all subtasks,
Veshy can only hold up to cue balls at any time.
Veshy can borrow money to buy cue balls (go into debt), but he can never sell more cue balls than he possesses. Veshy has to wait at least 1 month before selling the cue balls he buys.
Note for Python users: To pass this question using Python you must select the PyPy interpreter instead of the normal one.
Subtask 1 [20%]
Subtask 2 [30%]
Subtask 3 [50%]
No additional constraints.
The first line will contain two space-separated integers, and . The following lines will contain 4 space-separated integers, , , , and for month .
Output the maximum profit Veshy can make by the end of the month period.
Sample Input 1
3 1 5 3 1 2 3 4 3 2 1 5 2 10
Sample Output 1
Explanation of Sample Input 1
Veshy can buy cue balls on the first month for a price of dollar each. He can then hold on to these cue balls until the last month, where he can sell them for a price of dollars each. Since the cost of maintenance is dollar, and Veshy is holding onto those cue balls for months, Veshy's total profit would be dollars.
Sample Input 2
3 100 2 2 100 1 10 5 100 1 1 5 10000 1
Sample Output 2
Explanation of Sample Input 2
Here, the best move that Veshy could make is to not invest in the market at all! Buying any amount of cue balls on any month will always result in a loss.