#P5961. Coin Collector and Change
Coin Collector and Change
Coin Collector and Change
A country circulates n types of coins, which include the 1‐cent coin. In addition, there is a paper note worth K cents; its value exceeds that of any coin. A coin collector wishes to collect one sample of every coin denomination he does not already have. At home he already owns some coins, but he carries only a single K-cent note when he goes shopping.
The shop sells exactly K-1 items, priced at 1 cent, 2 cents, ..., up to K-1 cents. When a customer pays, the shop returns change using the following greedy algorithm:
- Let the total change required be A cents.
- Find the coin with the largest denomination that does not exceed A, say of value B cents.
- Give the customer one coin of value B and update A by setting A = A - B.
- If A = 0, the algorithm stops; otherwise, return to step 2.
The collector intends to purchase one item using his K-cent note. For a chosen item priced at p (where 1 ≤ p ≤ K-1), he will receive change amounting to K-p cents. The coins returned (possibly with repetitions) constitute his "loot" from which he only cares about the distinct coin types that he did not already own.
Your task is to determine:
- The maximum number of new coin types (i.e. coin denominations he does not already possess) he can acquire via the change, over all possible choices of p.
- Among all purchase prices that yield this maximum, what is the maximum product price he can pay?
- The first line contains two integers n and K, where n is the number of coin denominations and K is the value of the paper note.
- The second line contains n space-separated integers representing the coin denominations available in increasing order. It is guaranteed that 1 is among these coins and that every coin denomination is less than K.
- The third line begins with an integer m (with 0 ≤ m ≤ n) representing the number of coin types the collector already owns, followed by m distinct space-separated integers that are among the available coin denominations.
- The maximum number of new coin types the collector can acquire from the change.
- The maximum product price (among those yielding the maximum new coin count) he can pay.
Note that the coins available in the country are given in increasing order, are distinct, and the smallest coin is 1 cent. Also, the note's value K is strictly larger than any coin denomination.
inputFormat
The input consists of three lines:
outputFormat
Output two integers separated by a space:
sample
3 10
1 3 4
1 1
2 3