Under construction

Still being built. Things may change

Guess the Random Number

Interview

Time limit1sMemory limit1024 MB

Summary
Given member weights and the winners in draw order, print a ticket index for each draw that makes that member win, using the remaining tickets.
Level

Medium4 of 10

Topics
Prefix sum, Array
Solved
No attempts yet

Problem

A lottery at Youngkwa Day awards prizes to members who solved many problems during one semester.

Each member gets a weight that raises their chance of winning. If a member solved pp problems, their weight is pp.

There are NN members who solved at least one problem this semester. They are numbered from 11 to NN.

The lottery picks M(1≤M≤N)M(1 \leq M \leq N) winners out of the NN members. It starts with i=1i=1 and runs as follows.

  1. Going through members 11 to NN in order, add each member's ticket to the list WW as many times as their weight. For example, if there are 3 members with weights 2, 1, 32,\ 1,\ 3, then W=[T1,T1,T2,T3,T3,T3]W = [T_1, T_1, T_2, T_3, T_3, T_3]. (TkT_k is the ticket of member kk.)
  2. Pick one integer from 11 to ∣W∣|W| at random and call it XiX_i.
  3. Select the XiX_i-th ticket from the front of WW to pick one winner.
  4. Remove all tickets of the member who just won from WW.
  5. If i≠Mi \neq M, increase ii by 11 and go back to step 2. Otherwise, end the lottery.

The winners' numbers are given in the order they were drawn. Guess which random numbers were drawn, in order.

Input

The first line contains NN and MM, separated by a space. (1≤N≤500 000;1≤M≤N)(1 \leq N \leq 500\,000; 1 \leq M \leq N)

The second line contains the weight pp of each member, separated by spaces. There are NN values. (1≤p≤1 000)(1 \leq p \leq 1\,000)

The third line contains the member numbers kk of the winners, in the order they were drawn, separated by spaces. There are MM values. (1≤k≤N)(1 \leq k \leq N)

All input values are integers.

Output

Print the sequence XX of random numbers drawn during the lottery, separated by spaces.

If more than one sequence is possible, print any one of them.

Examples1

  1. Example 1

    Input
    5 3
    1 2 3 4 5
    3 5 1
    
    Expected output
    5 9 1