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

#PS0000179. Chọn chỉ số theo trọng số — phiếu cho trước (Random Pick with Weight — Recorded Draws)

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ố — phiếu cho trước (Random Pick with Weight — Recorded Draws)

Random Pick with Weight — Recorded Draws

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given positive integer weights w0,…,wn−1w_0,\ldots,w_{n-1}, let T=∑i=0n−1wiT=\sum_{i=0}^{n-1}w_i. Number TT tickets from 1 through TT, assigning the first w0w_0 to index 0, the next w1w_1 to index 1, and so on. For each supplied drawn ticket xx, identify its owner index. Draws are independent, with replacement. This deterministic adaptation of weighted random sampling supplies the drawn integers in the input; submissions must not generate random numbers.

Input

The first line contains n,qn,q. The second contains nn weights w0,…,wn−1w_0,\ldots,w_{n-1}. The third contains qq tickets x1,…,xqx_1,\ldots,x_q.

Output

Print qq lines, each giving the corresponding owner index (0 through n−1n-1).

Subtasks

  • Subtask 1 (20 points): 1≤n≤5001\le n\le 500; 1≤q≤5001\le q\le 500; 1≤wi≤1051\le w_i\le10^5; 1≤xj≤T1\le x_j\le T.
  • Subtask 2 (30 points): 1≤n≤100001\le n\le 10000; 1≤q≤100001\le q\le 10000; 1≤wi≤1051\le w_i\le10^5; 1≤xj≤T1\le x_j\le T; w0=⋯=wn−1w_0=\cdots=w_{n-1}.
  • Subtask 3 (50 points): 1≤n≤100001\le n\le 10000; 1≤q≤100001\le q\le 10000; 1≤wi≤1051\le w_i\le10^5; 1≤xj≤T1\le x_j\le T.

Example 1

Input

2 4
1 3
1 2 4 1

Output

0
1
1
0

Explanation

Ticket 1 belongs to index 0; tickets 2,3,4 belong to index 1. Drawing ticket 1 again still gives zero.

Example 2

Input

3 5
2 1 3
1 2 3 4 6

Output

0
0
1
2
2

Explanation

The three ticket intervals are [1,2], [3,3], [4,6].