Ctrl K

Growing Capital Project by Project

Hard
A studio starts with funds and has a list of n projects. Project i can be started only if the current capital is at least entry[i]; once finished, it brings a net profit of gains[i], which is added to the capital. The threshold entry[i] itself is not spent. Before the season ends, the studio can complete at most slots distinct projects, one at a time. Each project can be taken at most once. Choose the projects and their order to make the final capital as large as possible, and return that capital.
Example 1
slots=2, funds=1, gains=[5, 2, 9], entry=[3, 1, 6] → 8

With 1 in hand only project 1 is open: capital becomes 3. Now project 0 (+5) is open, giving 8. Project 2 with its gain of 9 never becomes reachable within two slots.

Example 2
slots=3, funds=0, gains=[4, 1, 2], entry=[5, 0, 1] → 3

0 → project 1 → 1 → project 2 → 3. Project 0 needs 5, which is out of reach, so the third slot goes unused. The answer is 3.

Constraints
  • 1 ≤ n ≤ 10⁵, len(entry) = len(gains) = n
  • 1 ≤ slots ≤ 10⁵
  • 0 ≤ funds ≤ 10⁹
  • 0 ≤ gains[i] ≤ 10⁴
  • 0 ≤ entry[i] ≤ 10⁹
  • The answer is less than 2³¹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMost Orders by Their Deadlines HardNarrowest Range Touching Every List HardCrew Paid by Skill HardThe Slowest Sets the Squad's Pace HardScore Spread of Parcel Groups Hard
loading Python…
Input
slots=2, funds=1, gains=[5, 2, 9], entry=[3, 1, 6]
Expected
8

Run checks the first 3 tests in your browser. Submit checks all 131 tests on the server, hidden ones included.