NOCHANGE - No Change

Though it might be hard to imagine, the inhabitants of a small country Additivia do not know of such thing as change, which probably has to do with them not knowing subtraction either. When they buy something, they always need to have the exact amount of addollars, their currency. The only other option, but not a really attractive one, is over-paying.

Professor Adem, one of the Additivian mathematicians came up with an algorithm for keeping a balanced portfolio. The idea is the following. Suppose you have more coins of value $v_1$ than coins of value $v_2$. In this case you should try to spend at least as many coins of value $v_1$ as those of value $v_2$ on any buy you make. Of course spending too many $v_1$ coins is not a good idea either, but to make the algorithm simpler professor Adem decided to ignore the problem. The algorithm became an instant hitand professor Adem is now designing a kind of "electronic portfolio" with built-in Adem's algorithm. All he needs now is a software for these machines, that will decide whether a given amount ofaddollars can be paid using a given set of coins according to the rules of Adem's algorithm. Needless to say, you are his chosen programmer for the task.


Write a program that reads the description of a set of coins and an amount of addollars to be paid, and determines whether you can pay that amount according to Professor Adem's rules.


The input starts with the amount of addollars to be paid $x$, where $1 \le x \le 100,000$. The number of different coin values $k$ follows, where $1 \le k \le 5$. The values of the coins $v_1, \ldots, v_k$ follow, where $1 \le v_i \le 10,000$.

Notice that the order among coin values is significant: you need to spend at least as many coins of value $v_1$ as coins of value $v_2$, at least as many coins of value $v_2$ as those of value $v_3$, and so on. You may assume that you have a sufficiently large number of coins of each value.


Your program should output for each test case either a single word "YES", if the given amount can be paid according to the rules, or a single word "NO" otherwise.


13 3 9 2 1


hide comments
smso: 2019-04-26 04:52:59

helpful testcases:
18 3 4 4 1
18 2 4 1

masterassassin: 2019-02-24 16:27:19

AC finally..
I take WA on test 10 because i didn't know that we could take some of money not all..
try this test if you take WA also :
11 3 9 2 1
here we could take nine and two only

karan_yadav: 2018-07-05 07:52:54

For all those who couldn't understand the problem statement:
Suppose you have coins of values $1, $2 and $3 and let the number of coins of each value used be n1, n2, n3 then n1 >= n2 >= n3. You have to check if you can pay the required amount using given coins and following above condition.

aman_sachin200: 2018-06-12 00:46:10

Nice one!!!!Used Recursion+Memoization+Backtracking to keep count of coins of different kinds used!!! :P

Last edit: 2018-06-12 00:49:52
shahzada: 2018-03-13 06:23:03

Easy One! Should be moved to the tutorial section.

sonudoo: 2017-07-01 15:43:27


razor123: 2017-04-24 20:56:59

Nice variation of coin change. O(x*k)

code_caeser: 2016-10-21 22:16:03

nice dp problem...something new...recursion+memorisation...AC in 1st go!!:)

conquistador: 2016-10-01 20:25:43

getting wrong answer in test case 10 any hints?

vignesh97: 2016-08-04 19:20:07

Good problem. AC in 1st go :)

Added by:overwise
Time limit:0.644s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:ACM ICPC -- SWERC 2001