#P9789. [ROIR 2020] ATM (Day 2)

[ROIR 2020] ATM (Day 2)

Problem Description

Translated from ROIR 2020 Day 2 T3. Банкомат, translated by ksyx.

You are developing an ATM with multiple denominations, called the Universal ATM. Specifically, there are nn kinds of banknotes with denominations a1, a2, , ana_1,~a_2,~\ldots,~a_n sorted in increasing order, i.e. for i>2i>2, ai1<aia_{i-1}<a_i. In addition, it is guaranteed that a1=1a_1=1.

Suppose a customer wants to withdraw a total amount of cc. Your ATM will distribute banknotes using the following algorithm: each time, choose the largest denomination such that the total amount given to the user after choosing it does not exceed cc, until the total amount reaches cc. Since there are banknotes of denomination 11, we can see that this algorithm always finishes.

To evaluate the efficiency of this algorithm, you want to know, among all requests with total amount not exceeding bb, what is the maximum number of banknotes that may be needed. Because business scenarios are different, you need to answer qq queries for b1, b2, , bqb_1,~b_2,~\ldots,~b_q.

Your task is to write a program that, given the list of denominations, determines for different business scenarios the maximum number of banknotes that may be needed.

Input Format

The first line contains an integer nn, the number of denominations.

The next line contains nn integers aia_i.

The third line contains an integer qq, the number of queries.

The next qq lines each contain an integer bib_i.

Output Format

For each query, output two integers: the total amount withdrawn by the customer in the case where the number of banknotes is maximized, and the number of banknotes. If multiple withdrawal requests not exceeding the maximum total amount in the input correspond to this number of banknotes, output any one of them.

4
1 5 10 50
3
2
8
50
2 2
8 4
49 9

Hint

Sample 1 Explanation

One feasible solution for each query is as follows:

2=1+12=1+1, 8=5+1+1+18=5+1+1+1, 49=10+10+10+10+5+1+1+1+149=10+10+10+10+5+1+1+1+1.

Constraints

For 100%100\% of the testdata, 1n2×1051\le n\le 2\times 10^5, 1=a1<a2<<an10181=a_1<a_2<\ldots<a_n\le10^{18}, 1q2×1051\le q\le 2\times 10^5, 1bi10181\le b_i\le 10^{18}.

Details of each subtask are as follows:

Subtask ID Points Limits
11 1313 1n500, q5, 1ai,bi5001 \le n \le 500,~q\le 5,~1\le a_i,b_i\le500
22 1818 n=60, q5, ai=2i1n=60,~q\le 5,~a_i=2^{i-1}
33 2020 q5, bi2×105q\le 5,~b_i\le 2\times 10^5
44 2121 q5q\le 5
55 2828

Translated by ChatGPT 5