class Solution:
def assignTasks(self, servers: List[int], tasks: List[int]) -> List[int]:
""" tc O(SlgS+T*lgT) sc O(S)
main idea: two PQ
"""
available = [[w,j] for j,w in enumerate(servers)] # (weight,idex)
unava = [] # (available at time ,w, idx)
heapq.heapify(available)
cur_t = 0
n = len(tasks)
res = []#[-1] *n
for i in range(n):
cur_t = max(i,cur_t) # current time can be larger than i because there may be no available server at cur_t
if len(available) == 0: # need to wait until earliest available time in unavailable queue
cur_t = unava[0][0]
while unava and cur_t >= unava[0][0]:
tt,w,idx = heappop(unava)
heappush(available,[w,idx])
w,idx = heappop(available)
new_available_time = cur_t + tasks[i]
res.append(idx)
heappush(unava,[new_available_time,w,idx])
return res