<p>You are given two <strong>0-indexed</strong> integer arrays <code>servers</code> and <code>tasks</code> of lengths <code>n</code>​​​​​​ and <code>m</code>​​​​​​ respectively. <code>servers[i]</code> is the <strong>weight</strong> of the <code>i<sup>​​​​​​th</sup></code>​​​​ server, and <code>tasks[j]</code> is the <strong>time needed</strong> to process the <code>j<sup>​​​​​​th</sup></code>​​​​ task <strong>in seconds</strong>.</p>

<p>Tasks are assigned to the servers using a <strong>task queue</strong>. Initially, all servers are free, and the queue is <strong>empty</strong>.</p>

<p>At second <code>j</code>, the <code>j<sup>th</sup></code> task is <strong>inserted</strong> into the queue (starting with the <code>0<sup>th</sup></code> task being inserted at second <code>0</code>). As long as there are free servers and the queue is not empty, the task in the front of the queue will be assigned to a free server with the <strong>smallest weight</strong>, and in case of a tie, it is assigned to a free server with the <strong>smallest index</strong>.</p>

<p>If there are no free servers and the queue is not empty, we wait until a server becomes free and immediately assign the next task. If multiple servers become free at the same time, then multiple tasks from the queue will be assigned <strong>in order of insertion</strong> following the weight and index priorities above.</p>

<p>A server that is assigned task <code>j</code> at second <code>t</code> will be free again at second <code>t + tasks[j]</code>.</p>

<p>Build an array <code>ans</code>​​​​ of length <code>m</code>, where <code>ans[j]</code> is the <strong>index</strong> of the server the <code>j<sup>​​​​​​th</sup></code> task will be assigned to.</p>

<p>Return <em>the array </em><code>ans</code>​​​​.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> servers = [3,3,2], tasks = [1,2,3,2,1,2]
<strong>Output:</strong> [2,2,0,2,1,2]
<strong>Explanation: </strong>Events in chronological order go as follows:
- At second 0, task 0 is added and processed using server 2 until second 1.
- At second 1, server 2 becomes free. Task 1 is added and processed using server 2 until second 3.
- At second 2, task 2 is added and processed using server 0 until second 5.
- At second 3, server 2 becomes free. Task 3 is added and processed using server 2 until second 5.
- At second 4, task 4 is added and processed using server 1 until second 5.
- At second 5, all servers become free. Task 5 is added and processed using server 2 until second 7.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> servers = [5,1,4,3,2], tasks = [2,1,2,4,5,2,1]
<strong>Output:</strong> [1,4,1,4,1,3,2]
<strong>Explanation: </strong>Events in chronological order go as follows: 
- At second 0, task 0 is added and processed using server 1 until second 2.
- At second 1, task 1 is added and processed using server 4 until second 2.
- At second 2, servers 1 and 4 become free. Task 2 is added and processed using server 1 until second 4. 
- At second 3, task 3 is added and processed using server 4 until second 7.
- At second 4, server 1 becomes free. Task 4 is added and processed using server 1 until second 9. 
- At second 5, task 5 is added and processed using server 3 until second 7.
- At second 6, task 6 is added and processed using server 2 until second 7.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>servers.length == n</code></li>
	<li><code>tasks.length == m</code></li>
	<li><code>1 &lt;= n, m &lt;= 2 * 10<sup>5</sup></code></li>
	<li><code>1 &lt;= servers[i], tasks[j] &lt;= 2 * 10<sup>5</sup></code></li>
</ul>

class Solution:
    def assignTasks(self, servers: List[int], tasks: List[int]) -> List[int]:
        # server: [3,3,2]
        # tasks: [1,2,3,2,1,2]
        # results[j]:server
        # time==task index
        # a queue of task
        # a queue of free servers
        # add server queue first 
        # server empty queue [end time, index]
        # for time in range(n):
        # process_queue:add to preocess queue[time,index] #nlogn
        # while len(empty queue)!=0 and process queue!=0 #nlogm
        # add to server queue, with updated time
        # result[j]=server_index
        # if elements in the severqueue time <curtime: add to empty queue
        # while empy queue!=0 and process queue!=0
        # add to server queue, with updated time
        # result[j]=server_index
        # return results
        # if elements in the severqueue time <curtime: add to empty queue
        # time O(mlogm+nlogn+nlogm)
        import heapq 
        
        # weight index
        empty_server=[[servers[i],i] for i in range(len(servers))]
        heapq.heapify(empty_server)
        
        # end_time, weight, index
        run_server=[]

        result=[-1 for _ in range(len(tasks))]
        queue=collections.deque()
        for i in range(len(tasks)):
            while len(run_server)!=0 and run_server[0][0]<=i:
                serv=heappop(run_server)
                heappush(empty_server,serv[1:])
            
            # time, index
            queue.append([tasks[i],i])
            while len(empty_server)!=0 and len(queue)!=0:
                    cur=queue.popleft()
                    # weight, index
                    serv=heapq.heappop(empty_server)
                    result[cur[1]]=serv[1]
                    #time, weight, index
                    heapq.heappush(run_server,[i+cur[0],serv[0],serv[1]])

        #server: jobs
        #queue: not empty:
        #current time=i

        while len(queue)!=0:
            cur=queue.popleft()
            # weight, index
            serv=heapq.heappop(run_server)
            result[cur[1]]=serv[2]
            #time, weight, index
            heapq.heappush(run_server,[serv[0]+cur[0],serv[1],serv[2]])
            
        return result  
1561