Canonical Coin Systems
Time limit2sMemory limit512 MB
Given a sorted coin system, decide whether the greedy algorithm always makes change with the fewest coins, or whether some amount is a counterexample.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Math, Number theory
- Solved
- No attempts yet
Problem
A coin system is a finite nonempty set of distinct positive integers. Each element is a coin value, also called a denomination, in a real or imagined monetary system. The coin system in common use in Canada is , where 1 is a 1 cent coin and 200 is a 200 cent (2 dollar) coin. For any coin system , assume there is an unlimited supply of coins of each denomination, and assume that contains 1, which guarantees that any positive integer can be written as a sum of values in with repetition allowed.
Cashiers all over the world face and solve the following problem. Given a coin system and a positive integer amount owed to a customer, what is the smallest number of coins required to dispense exactly that amount? Suppose a cashier in Canada owes a customer 83 cents. One solution is , that is 8 coins, but it is not optimal, since the cashier could dispense instead, that is 7 coins, which is optimal here. The Canadian coin system has the property that the greedy algorithm always yields an optimal solution, as do the coin systems used in most countries. The greedy algorithm repeatedly chooses a coin of the largest denomination that is at most the amount still owed, until the amount owed reaches zero. A coin system for which the greedy algorithm is always optimal is called canonical.
Given a coin system , determine whether is canonical or non-canonical. If is non-canonical then it has at least one counterexample, that is a positive integer such that the minimum number of coins required to dispense exactly is less than the number of coins used by the greedy algorithm. One non-canonical coin system is , for which 6 is a counterexample: the greedy algorithm yields (3 coins), but an optimal solution is (2 coins). A useful fact, due to Dexter Kozen and Shmuel Zaks, is that if a coin system is non-canonical, then its smallest counterexample is less than the sum of the two largest denominations.
Input
Input consists of a single case. The first line contains an integer (), the number of denominations in the coin system. The next line contains the denominations as space separated integers , where and .
Output
Print canonical if the coin system is canonical, or non-canonical if the coin system is non-canonical.