A10615 | Too Much Money
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Alfred wants to buy a toy moose that costs $c$ dollars. The store doesn’t give change, so he must give the store exactly $c$ dollars, no more and no less. He has $n$ coins. To make $c$ dollars from his coins, he follows the following algorithm: let $S$ be the set of coins being used. $S$ is initially empty. Alfred repeatedly adds to $S$ the highest-valued coin he has such that the total value of the coins in $S$ after adding the coin doesn’t exceed $c$ . If there is no such coin, and the value of the coins in $S$ is still less than $c$ , he gives up and goes home. Note that Alfred never removes a coin from $S$ after adding it.
As a programmer, you might be aware that Alfred’s algorithm can fail even when there is a set of coins with value exactly $c$ . For example, if Alfred has one coin worth $3, one coin worth$4, and two coins worth $5, and the moose costs$12, then Alfred will add both of the $5 coins to$ S $and then give up, since adding any other coin would cause the value of the coins in$ S $to exceed$12. Of course, Alfred could instead combine one $3 coin, one$4 coin, and one $5 coin to reach the total.
Bob tried to convince Alfred that his algorithm was flawed, but Alfred didn’t believe him. Now Bob wants to give Alfred some coins (in addition to those that Alfred already has) such that Alfred’s algorithm fails. Bob can give Alfred any number of coins of any denomination (subject to the constraint that each coin must be worth a positive integer number of dollars). There can be multiple coins of a single denomination. He would like to minimize the total value of the coins he gives Alfred. Please find this minimum value. If there is no solution, print "Greed is good". You can assume that the answer, if it exists, is positive. In other words, Alfred's algorithm will work if Bob doesn't give him any coins.
As a programmer, you might be aware that Alfred’s algorithm can fail even when there is a set of coins with value exactly $c$ . For example, if Alfred has one coin worth $3, one coin worth$4, and two coins worth $5, and the moose costs$12, then Alfred will add both of the $5 coins to$ S $and then give up, since adding any other coin would cause the value of the coins in$ S $to exceed$12. Of course, Alfred could instead combine one $3 coin, one$4 coin, and one $5 coin to reach the total.
Bob tried to convince Alfred that his algorithm was flawed, but Alfred didn’t believe him. Now Bob wants to give Alfred some coins (in addition to those that Alfred already has) such that Alfred’s algorithm fails. Bob can give Alfred any number of coins of any denomination (subject to the constraint that each coin must be worth a positive integer number of dollars). There can be multiple coins of a single denomination. He would like to minimize the total value of the coins he gives Alfred. Please find this minimum value. If there is no solution, print "Greed is good". You can assume that the answer, if it exists, is positive. In other words, Alfred's algorithm will work if Bob doesn't give him any coins.
输入格式
The first line contains $c$ ( $1<=c<=200000$ ) — the price Alfred wants to pay. The second line contains $n$ ( $1<=n<=200000$ ) — the number of coins Alfred initially has. Then $n$ lines follow, each containing a single integer $x$ ( $1<=x<=c$ ) representing the value of one of Alfred's coins.
输出格式
If there is a solution, print the minimum possible total value of the coins in a solution. Otherwise, print "Greed is good" (without quotes).
输入输出样例
输入 #1
12 3 5 3 4
输出 #1
5
输入 #2
50 8 1 2 4 8 16 37 37 37
输出 #2
Greed is good
In the first sample, Bob should give Alfred a single coin worth $5. This creates the situation described in the problem statement.
In the second sample, there is no set of coins that will cause Alfred's algorithm to fail.
In the second sample, there is no set of coins that will cause Alfred's algorithm to fail.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted