502. IPO (Hard) - TengnanYao/daily_leetcode GitHub Wiki

class Solution:
    def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
        arr = sorted(zip(capital, profits))
        heap = []
        i = 0
        for _ in range(k):
            while i < len(arr) and arr[i][0] <= w:
                heapq.heappush(heap, -arr[i][1])
                i += 1
            if not heap:
                return w
            w -= heapq.heappop(heap)
        return w