Grow the Fund
You start with capital and may complete at most k projects, one after another. Project i can only be started while you hold at least needs[i] capital, and completing it adds gains[i] to your capital; the capital it needs is not spent. Each project can be completed at most once.
Return the largest capital you can hold after completing at most k projects. The largest capital is unique, so no tie rule is needed.
Example 1
With 0 capital only project 0 is startable; it brings 1. Then projects 1 and 2 are both startable, and project 2 pays more: 0 + 1 + 3 = 4.
Example 2
Project 0 unlocks project 1, which unlocks project 2: 0 + 1 + 2 + 3 = 6.
Constraints
0 <= capital <= 10000 <= k <= 1000 <= len(needs) <= 100len(gains) == len(needs)0 <= needs[i] <= 10000 <= gains[i] <= 1000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.