All problems

Problem 906

Process Tasks Using Servers

Medium · 20 pts · Array, Heap, Simulation

Problem

Servers have weights, and task j arrives at second j and needs TASKS[j] seconds. A free server is chosen for each waiting task in arrival order: the one with the smallest weight, then the smallest index. If no server is free, tasks wait until one frees up and are then assigned in order. Print the server index used for each task. Keep one heap of free servers (weight, index) and one of busy servers (free time, weight, index).

Input

The first line contains N and M. The second line contains the N server weights. The third line contains the M task durations.

Output

Print M integers separated by spaces: the server index for each task.

Constraints

1 <= N, M <= 100000; 1 <= weights, durations <= 2 × 10^5

Example input

3 6
3 3 2
1 2 3 2 1 2

Example output

2 2 0 2 1 2

Explanation

With servers of weight 3 3 2 and tasks 1 2 3 2 1 2, task 0 goes to server 2 (lightest) at second 0; at second 1 server 2 is free again and takes task 1; and so on, giving 2 2 0 2 1 2.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Minimum Number of K Consecutive Bit Flips Next: Total Cost to Hire K Workers