Suppose a company will start its IPO soon. In order to sell a good price of its shares to Venture Capital, the company would like to work on some projects to increase its capital before the IPO. Since it has limited resources, it can only finish at most k distinct projects before the IPO. Help the company design the best way to maximize its total capital after finishing at most k distinct projects. You are given n projects where the ith project has a pure profit profits[i] and a minimum capital of capital[i] is needed to start it. Initially, you have w capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital. Pick a list of at most k distinct projects from given projects to maximize your final capital, and return the final maximized capital.
Example 1
Input: k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
Output: 4
Explanation: With 0 capital, only project 0 (capital 0) can start, giving a profit of 1, so w = 1. Now projects 1 and 2 are both affordable, and the more profitable one, project 2 (+3), brings w to 4.
Example 2
Input: k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
Output: 6
Explanation: Start project 0 (w = 1), which unlocks project 1 (w = 3), which unlocks project 2 (w = 6). All three projects fit within k = 3.
Example 3
Input: k = 1, w = 0, profits = [1,2,3], capital = [1,1,2]
Output: 0
Explanation: Every project needs at least 1 capital and we start with 0, so nothing can be started and the capital stays 0.
At each of the k rounds, among all projects you can currently afford, always take the one with the highest profit. Sort projects by required capital so you can unlock newly-affordable ones as your capital grows, and keep affordable profits in a max-heap.
Run checks all cases above. Submit evaluates all test cases.