Greedy Coin Exchange
Time limit1sMemory limit64 MB
Given sorted coin denominations starting with 1, decide whether the greedy largest-coin-first method always uses the fewest coins for every amount.
- Level
Medium7 of 10
- Topics
- Greedy, Dynamic programming, Number theory, Math
- Solved
- No attempts yet
Problem
The coin change problem is a standard first example of dynamic programming. It reads as follows.
- To make change for won with coins of denominations , what is the smallest number of coins needed?
Byeongchan wants to solve it with a simple method. His method is as follows.
- At each step, add one coin of the largest denomination that does not exceed the amount still missing. Repeat until the remaining amount is 0.
Byeongchan's method is not always optimal. To make 8 won from coins of 1, 4, and 6, his method uses one 6 and two 1s, three coins in total. Two 4s do it with two coins.
Byeongchan realized that his method works for some sets of denominations and fails for others. Given the denominations, write a program that decides whether his method produces the smallest number of coins for every .
Input
The first line contains the number of denominations . ()
The second line contains the denominations separated by spaces. They satisfy .
Output
Print Yes if Byeongchan's method is always optimal, and No otherwise.