#P5956. [POI 2017] Podzielno

[POI 2017] Podzielno

Problem Description

In base BB, for each digit i[0,B)i \in [0,B), you have aia_i copies. You need to use these digits to form the largest base-BB number XX (no leading zeros, and you do not need to use all digits), such that XX is a multiple of B1B-1. There are qq queries. For each query, ask what the digit at position kk of XX is in base BB (the least significant digit is position 00).

Input Format

The first line contains two positive integers B,qB, q.

The second line contains BB positive integers a0,a1,a2,...,aB1a_0, a_1, a_2, ..., a_{B-1}.

The next qq lines each contain one integer kk, representing a query.

Output Format

Output qq lines, each containing one integer, answering the queries in order. If that position does not exist, output 1-1.

3 3
1 1 1
0
1
2
0
2 
-1

Hint

Constraints: for 100%100\% of the testdata, 2B1062 \le B \le 10^6, 1q1051 \le q \le 10^5, 1ai1061 \le a_i \le 10^6, 0k10180 \le k \le 10^{18}.

Translated by ChatGPT 5