Description

Maximize capital after completing at most k projects. Each project has capital requirement and profit.

Examples

Input:k=2, w=0, profits=[1,2,3], capital=[0,1,1]
Output:4
Explanation:

Do projects 0 and 2 for max capital.

Input:k=2, w=0, profits=[1], capital=[1]
Output:1
Explanation:

Edge case with a single-element array.

Input:k=3, w=2, profits=[4,1,2,3], capital=[3,1,2,4]
Output:9
Explanation:

Start with w=2. Project 1 (capital=1, profit=1) brings w to 3. Project 2 (capital=2, profit=2) brings w to 5. Project 0 (capital=3, profit=4) brings w to 9. Project 3 requires capital=4 but only 5 is available after the first two projects, so it could also be done. Maximum capital: 9.

Constraints

  • 1 ≤ k ≤ 10⁵
  • 0 ≤ w ≤ 10⁹

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!