A coin system is a finite (nonempty) set of
distinct positive integers corresponding to coin values, also
called denominations, in a real or imagined monetary
system. For example, the coin system in common use in Canada is
,
where corresponds to a
-cent coin and corresponds to a -cent (-dollar) coin. For any coin
system , we assume that
there is an unlimited supply of coins of each denomination, and
we also assume that
contains , since this
guarantees that any positive integer can be written as a sum of
(possibly repeated) values in .
Cashiers all over the world face (and solve) the following
problem: For a given coin system and a positive integer amount
owed to a customer, what is the smallest number of coins
required to dispense exactly that amount? For example, suppose
a cashier in Canada owes a customer cents. One possible solution is
,
i.e., coins, but this
is not optimal, since the cashier could instead dispense
, i.e.,
coins (which
is optimal in this case). Fortunately, the Canadian
coin system has the nice property that the greedy
algorithm always yields an optimal solution, as do the
coin systems used in most countries. The greedy algorithm
involves repeatedly choosing a coin of the largest denomination
that is less than or equal to the amount still owed, until the
amount owed reaches zero. A coin system for which the greedy
algorithm is always optimal is called canonical.
Your challenge is this: Given a coin system ,
determine whether is
canonical or non-canonical. Note that if is non-canonical then there exists
at least one counterexample, i.e., 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. An example of a non-canonical
coin system is , for which is
a counterexample, since the greedy algorithm yields
( coins), but an optimal solution is
( coins). A useful fact (due to
Dexter Kozen and Shmuel Zaks) is that if is non-canonical, then the
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
Output “canonical” if the coin
system is canonical, or “non-canonical” if the coin system is
non-canonical.
Sample Input 1 |
Sample Output 1 |
4
1 2 4 8
|
canonical
|
Sample Input 2 |
Sample Output 2 |
3
1 5 8
|
non-canonical
|
Sample Input 3 |
Sample Output 3 |
6
1 5 10 25 100 200
|
canonical
|