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
Constraints
For all subtasks:
Veshy can only hold up to
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.
Input Specification
The first line will contain two space-separated integers,
Output Specification
Output the maximum profit Veshy can make by the end of the
Sample Input 1
3 1
5 3 1 2
3 4 3 2
1 5 2 10
Sample Output 1
35
Explanation of Sample Output 1
Veshy can buy
Sample Input 2
3 100
2 2 100 1
10 5 100 1
1 5 10000 1
Sample Output 2
0
Explanation of Sample Output 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.
Comments