PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000174. Chọn chỉ số theo trọng số bằng vé (Random Pick with Weight)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Chọn chỉ số theo trọng số bằng vé (Random Pick with Weight)

Random Pick with Weight — Ticket Mapping

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

You are given nn positive weights w1,w2,…,wnw_1,w_2,\ldots,w_n corresponding to zero-based indices 00 through n−1n-1. There are W=∑i=1nwiW=\sum_{i=1}^{n}w_i tickets numbered 11 to WW. Index 00 owns the first w1w_1 tickets, index 11 owns the next w2w_2 tickets, and so forth; each index owns a consecutive ticket interval whose length equals its weight.

For each of the qq supplied ticket numbers r1,r2,…,rqr_1,r_2,\ldots,r_q, output the index owning that ticket.

PHOJ Extended adaptation: The original LeetCode 528 API independently generates random samples with probability proportional to weights. This version supplies the tickets and checks the exact ticket-to-index mapping. It does not test a contestant's random-number generator. If tickets are uniformly chosen from 11 to WW, the probability of obtaining an index is exactly proportional to its weight.

Input

The first line contains integers n,qn,q. The second line contains nn positive weights w1,w2,…,wnw_1,w_2,\ldots,w_n. The third line contains qq ticket numbers r1,r2,…,rqr_1,r_2,\ldots,r_q.

Output

Print qq lines. Line jj contains the zero-based index that owns ticket rjr_j.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le30; 1≤q≤501\le q\le50; 1≤wi≤1001\le w_i\le100 for every ii; 1≤rj≤∑i=1nwi1\le r_j\le\sum_{i=1}^{n}w_i for every jj.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; 1≤q≤20001\le q\le2000; 1≤wi≤1051\le w_i\le10^5 for every ii; 1≤rj≤∑i=1nwi1\le r_j\le\sum_{i=1}^{n}w_i for every jj.
  • Subtask 3 (50 points): 1≤n≤1041\le n\le10^4; 1≤q≤1041\le q\le10^4; 1≤wi≤1051\le w_i\le10^5 for every ii; 1≤rj≤∑i=1nwi1\le r_j\le\sum_{i=1}^{n}w_i for every jj.

Examples

Input

2 5
1 3
1 4 2 3 1

Output

0
1
1
1
0

Explanation

There are W=1+3=4W=1+3=4 tickets. Index 00 owns ticket 11, while index 11 owns tickets 2,3,42,3,4. Therefore the five given tickets map to indices 0,1,1,1,00,1,1,1,0 in that order.