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

#BS0000487. Đóng hộp khoai tây (Packing Potatoes)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đóng hộp khoai tây (Packing Potatoes)

Packing Potatoes

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

A conveyor delivers 1010010^{100} potatoes to Takahashi in order. Their weights repeat the list W0,W1,…,WN−1W_0,W_1,\ldots,W_{N-1}: potato number ii weighs W(i−1) mod NW_{(i-1)\bmod N}, where numbering starts at 11 and  mod N\bmod N denotes the remainder modulo NN.

Starting with an empty box, Takahashi places each arriving potato into it. As soon as the combined weight reaches or exceeds XX, he seals the box and starts another empty box. No potato may be skipped, and excess weight is not carried over to the next box.

For each of QQ queries, report how many potatoes are in sealed box number KiK_i. Boxes are numbered starting at 11 in sealing order. The supply is sufficient to seal at least KiK_i boxes for every query.

Input

  • The first line contains integers N,Q,XN,Q,X: the length of the repeating weight list, the number of queries and the sealing threshold.
  • The second line contains NN integers W0,W1,…,WN−1W_0,W_1,\ldots,W_{N-1} in cycle order.
  • Each of the next QQ lines contains an integer KiK_i, identifying the box in query ii.

Output

Print QQ lines. Line ii contains the number of potatoes in box KiK_i.

Subtasks

  • Subtask 1 (20 points): 1≤N≤501\le N\le 50; 1≤Q≤501\le Q\le 50; 1≤X≤10001\le X\le 1000; 1≤Wi≤1091\le W_i\le 10^9 for all 0≤i<N0\le i<N; 1≤Ki≤10001\le K_i\le 1000 for all 1≤i≤Q1\le i\le Q. All input values are integers; the supply suffices for every query.
  • Subtask 2 (30 points): 1≤N≤20001\le N\le 2000; 1≤Q≤20001\le Q\le 2000; 1≤X≤1091\le X\le 10^9; 1≤Wi≤1091\le W_i\le 10^9 for all 0≤i<N0\le i<N; 1≤Ki≤10121\le K_i\le 10^{12} for all 1≤i≤Q1\le i\le Q. All input values are integers; the supply suffices for every query.
  • Subtask 3 (50 points): 1≤N≤2⋅1051\le N\le 2\cdot10^5; 1≤Q≤2⋅1051\le Q\le 2\cdot10^5; 1≤X≤1091\le X\le 10^9; 1≤Wi≤1091\le W_i\le 10^9 for all 0≤i<N0\le i<N; 1≤Ki≤10121\le K_i\le 10^{12} for all 1≤i≤Q1\le i\le Q. All input values are integers; the supply suffices for every query.

Examples

Input 1

3 2 5
3 4 1
1
2

Output 1

2
3

Explanation 1

The sealing threshold is 55. The first box receives weights 3,43,4, with running totals 3,73,7, and contains 22 potatoes. The next box receives weights 1,3,41,3,4, with running totals 1,4,81,4,8. It reaches the threshold only after its third potato, giving the second answer 33.

Input 2

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

Output 2

4
5
5
5
5

Explanation 2

The first box takes weights 5,8,5,95,8,5,9, totaling 2727, so it contains 44 potatoes. Beginning with box two, the starting indices in the weight list are 4,8,2,5,9,3,6,14,8,2,5,9,3,6,1 (zero-based), then return to 44. Their potato counts are 4,4,3,4,4,3,5,34,4,3,4,4,3,5,3. All four large query indices are divisible by 88, corresponding to starting index 66, whose box contains 55 potatoes.