cswork / OAOpen practice workspace

AmazonOA #340

Schedule Requests to Servers

Estimated HardJudge-ready

Topics (AI-labelled)CachesSimulation

There are servers servers, numbered 0..servers−1. Process n requests in order: request i can only be assigned to a server numbered from 0 to requests[i] (inclusive), and it goes to the least busy one among them; if several are equally least busy, choose the one with the smallest number. A server's busyness is the number of requests already assigned to it, and assigned requests are never released. Output the server number each request is assigned to.

Input

The first line contains two integers servers and n; the second line contains n integers requests[0..n−1]. 1≤servers≤100000, 1≤n≤200000, 0≤requests[i]<servers.

Output

Output one line of n integers separated by spaces; the i-th is the server number assigned to request i.

Example 1

Input:

5 5
3 1 0 2 1

Output:

0 1 0 2 1

Example 2

Input:

3 4
2 2 2 2

Output:

0 1 2 0

Example 3

Input:

4 4
0 0 3 3

Output:

0 0 1 2

Example notes

Example 1: servers=5, requests=[3,1,0,2,1]. For request 0, every server in 0..3 is idle, so choose 0; for request 1, server 1 is less busy within 0..1, so choose 1; request 2 can only choose 0; for request 3, server 2 has 0 assignments within 0..2, so choose 2; for request 4, within 0..1 server 0 has 2 and server 1 has 1, so choose 1. Output 0 1 0 2 1. Example 2: all three servers are available, and the requests go to 0, 1 and 2 in turn; at the fourth request each of the three has 1, so choose 0. Example 3: the first two requests can only use server 0; after that server 1 has the fewest assignments, so choose 1, then 2.

Editorial

Loading solution…