#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 kinds of banknotes with denominations sorted in increasing order, i.e. for , . In addition, it is guaranteed that .
Suppose a customer wants to withdraw a total amount of . 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 , until the total amount reaches . Since there are banknotes of denomination , 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 , what is the maximum number of banknotes that may be needed. Because business scenarios are different, you need to answer queries for .
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 , the number of denominations.
The next line contains integers .
The third line contains an integer , the number of queries.
The next lines each contain an integer .
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:
, , .
Constraints
For of the testdata, , , , .
Details of each subtask are as follows:
| Subtask ID | Points | Limits |
|---|---|---|
Translated by ChatGPT 5