Problem 447822 · hard · Phase 04 Non-Linear Data Structures

Venture Rounds

heaps · greedy · sorting · two-phase selection

A fund starts with capital w and may back at most k projects, one after another. Project i requires the fund to hold at least costs[i] capital before backing it (the capital is not spent) and then adds profits[i] to the capital. Each project can be backed at most once.

Return the largest capital the fund can end with.

Examples

Input:  w = 1, k = 3, costs = [1, 3, 2, 5], profits = [2, 4, 3, 10]
Output: 17
Explanation: with 1 only project 0 is allowed: capital 3. Now projects 1 and 2 are allowed;
             take the profit 4: capital 7. Project 3 is now allowed: capital 17.

Input:  w = 2, k = 1, costs = [3, 4], profits = [5, 6]
Output: 2
Explanation: nothing is affordable, so the capital stays 2.

Constraints

  • 1 <= len(costs) == len(profits) <= 10**5, 0 <= w, costs[i], profits[i] <= 10**9
  • 1 <= k <= 10**5
  • Target complexity: O(n log n).

Goals

  • Unlock candidates in order of a threshold with a sorted pointer
  • Choose the best unlocked candidate with a max-heap
  • Stop early when nothing affordable remains
Starting Python…