502. IPO

Practice IPO for free on NudgeCode: Hard, Heap pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

You start with capital w and may complete at most k distinct projects, one after another. Project i needs at least capital[i] in hand to start, and finishing it adds profits[i] to your capital (starting a project costs nothing).

Return the largest capital you can end up with.

Example

k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]  ->  4

Start project 0 (capital becomes 1), then project 2 (capital becomes 4).

Constraints