2173. Change

单点时限: 2.0 sec

内存限制: 256 MB

Sue is waiting in line at the grocery store. Being in a hurry, she wants to pay with exact change when she gets to the front of the line. However, she does not know how much her items are going to cost; instead, she only knows an upper bound C on their total cost. Given a list of the various coins Sue has in her pocket, your goal is to determine the minimum number of coins she must take out in order to ensure that she can make exact change for every amount from 1 to C.

输入格式

The input test file will contain multiple cases. Each test case begins with a single line containing two integers C (where 1 ≤ C ≤ 1000000000) and m (where 1 ≤ m ≤ 1000), where C is the maximum amount for which Sue must be able to make change, and m is the number of unique coin denominations Sue has in her pocket. The next m lines each contain two numbers, vi (where 1 ≤ vi ≤ 1000) and ni (where 1 ≤ ni ≤ 1000), where vi is the value of the ith coin denomination, and ni is the number of coins of that denomination that Sue has in her pocket. Input is terminated by a single line containing the number 0; do not process this line.

输出格式

For each test case, either print a single line containing the number of coins Sue must use in order to make exact change for all amounts up to C, or print “Not possible” if exact change cannot always be made with any combination of coins in Sue’s pocket.

样例

Input
4 2
2 1
1 3
9 3
1 5
8 2
7 1
0
Output
3
Not possible

6 人解决,11 人已尝试。

8 份提交通过,共有 36 份提交。

7.6 EMB 奖励。

创建: 16 年,6 月前.

修改: 6 年,8 月前.

最后提交: 8 年,7 月前.

来源: Stanford Local 2007

题目标签