atcoder#ABC258E. [ABC258E] Packing Potatoes

[ABC258E] Packing Potatoes

Score : 500500 points

Problem Statement

1010010^{100} potatoes are coming from a conveyor belt one by one. The weights of the potatoes are described by a sequence W=(W0,,WN1)W = (W_0, \dots, W_{N-1}) of length NN: the weight of the ii-th potato coming is W(i1)modNW_{(i-1) \bmod N}, where (i1)modN(i-1) \bmod N denotes the remainder when i1i - 1 is divided by NN.

Takahashi will prepare an empty box and then pack the potatoes in order, as follows.

  • Pack the incoming potato into the box. If the total weight of the potatoes in the box is now XX or greater, seal that box and prepare a new empty box.

You are given QQ queries. In the ii-th query (1iQ)(1 \leq i \leq Q), given a positive integer KiK_i, find the number of potatoes in the KiK_i-th box to be sealed. It can be proved that, under the Constraints of the problem, there will be at least KiK_i sealed boxes.

Constraints

  • 1N,Q2×1051 \leq N, Q \leq 2 \times 10^5
  • 1X1091 \leq X \leq 10^9
  • 1Wi109(0iN1)1 \leq W_i \leq 10^9 \, (0 \leq i \leq N - 1)
  • 1Ki1012(1iQ)1 \leq K_i \leq 10^{12} \, (1 \leq i \leq Q)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN QQ XX

W0W_0 W1W_1 \ldots WN1W_{N-1}

K1K_1

\vdots

KQK_Q

Output

Print QQ lines. The ii-th line (1iQ)(1 \leq i \leq Q) should contain the answer to the ii-th query.

3 2 5
3 4 1
1
2
2
3

Before sealing the 22-nd box, Takahashi will do the following:

  • Prepare an empty box.
  • Pack the 11-st potato into the box. Now, the total weight of potatoes in the box is 33.
  • Pack the 22-nd potato into the box. Now, the total weight of potatoes in the box is 3+4=73 + 4 = 7, which is not less than X=5X = 5, so seal this box.
  • Prepare a new empty box.
  • Pack the 33-rd potato into the box. Now, the total weight of potatoes in the box is 11.
  • Pack the 44-th potato into the box. Now, the total weight of potatoes in the box is 1+3=41 + 3 = 4.
  • Pack the 55-th potato into the box. Now, the total weight of potatoes in the box is 1+3+4=81 + 3 + 4 = 8, which is not less than X=5X = 5, so seal this box.

The 11-st box sealed contains 22 potatoes, and the 22-nd box sealed contains 33 potatoes.

10 5 20
5 8 5 9 8 7 4 4 8 2
1
1000
1000000
1000000000
1000000000000
4
5
5
5
5