Least Energy to Finish Every Task
Each entry of tasks is [cost, needed]. Doing the task uses up cost units of energy, but you may only begin it while you have at least needed units.
You do the tasks one after another in any order you like. Return the least energy you must start with to finish them all.
Example 1
tasks [[3,5],[2,7],[4,4]]
returns 9
Start with 9: [2, 7] leaves 7, then [3, 5] leaves 4, then [4, 4] leaves 0. The tasks cost 9 in total, so less cannot work.
Example 2
tasks [[1,10],[1,10]]
returns 11
After the first task one unit is gone, and the second still asks for 10.
Example 3
tasks [[5,5]]
returns 5
One task: you need what it asks for.
Constraints
1 <= len(tasks) <= 301 <= cost <= needed <= 10000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.