1from heapq import heappush, heappop
2
3def findMaximizedCapital(k, w, profits, capital):
4 # Sort projects by capital
5 projects = sorted(zip(capital, profits))
6 max_heap = []
7 i = 0
8
9 for _ in range(k):
10 # Add all affordable projects to heap
11 while i < len(projects) and projects[i][0] <= w:
12 heappush(max_heap, -projects[i][1])
13 i += 1
14
15 # If no projects available, break
16 if not max_heap:
17 break
18
19 # Take most profitable project
20 w += -heappop(max_heap)
21
22 return w