CF725E.Too Much Money
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
阿尔弗雷德想买一个价值为 c 美元的玩具驼鹿。商店不找零,因此他必须恰好支付 c 美元,不能多也不能少。他手上有 n 枚硬币。为了凑出 c 美元,他采用如下算法:令 S 表示当前所选硬币的集合,初始时 S 为空集。阿尔弗雷德反复执行以下操作:在自己拥有的硬币中,选择面值最大的一枚硬币,使得将其加入 S 后,S 中所有硬币的总价值不超过 c;然后将该硬币加入 S。若不存在满足条件的硬币,且此时 S 中硬币的总价值仍小于 c,则他放弃购买,回家去。注意:一旦某枚硬币被加入 S,阿尔弗雷德便永远不会将其移除。
作为一名程序员,你可能已经意识到:即使存在一组硬币,其总价值恰好等于 c,阿尔弗雷德的算法仍可能失败。例如,若阿尔弗雷德有一枚 3 美元、一枚 4 美元和两枚 5 美元的硬币,而驼鹿售价为 12 美元,则阿尔弗雷德会先将两枚 5 美元硬币都加入 S(此时总和为 10),接着因再加入任何剩余硬币都会使总和超过 12 而放弃。但显然,他本可以选用一枚 3 美元、一枚 4 美元和一枚 5 美元的硬币来精确凑出 12 美元。
鲍勃曾试图说服阿尔弗雷德:他的算法是有缺陷的,但阿尔弗雷德并不相信。现在,鲍勃希望额外给阿尔弗雷德一些硬币(即在阿尔弗雷德已有的硬币之外再添加若干硬币),使得阿尔弗雷德的算法失败。鲍勃可以给阿尔弗雷德任意数量、任意面值的硬币(每枚硬币的面值必须为正整数美元),同一面值的硬币可以有多枚。他希望最小化所给硬币的总面值。请找出这个最小总面值。如果不存在这样的方案,请输出 "Greed is good"。你可以假设:若解存在,则该解一定为正数。换言之,若鲍勃不给阿尔弗雷德任何额外硬币,则阿尔弗雷德的算法一定能成功。
输入格式
The first line contains c (1 ≤ c ≤ 200 000) — the price Alfred wants to pay. The second line contains n (1 ≤ n ≤ 200 000) — 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.
第一行包含整数 c(1 ≤ c ≤ 200000)——阿尔弗雷德希望支付的金额。
第二行包含整数 n(1 ≤ n ≤ 200000)——阿尔弗雷德最初拥有的硬币数量。
接下来有 n 行,每行包含一个整数 x(1 ≤ x ≤ c),表示阿尔弗雷德的一枚硬币的面值。
输出格式
If there is a solution, print the minimum possible total value of the coins in a solution. Otherwise, print "Greed is good" (without quotes).
如果存在解,则输出解中硬币总价值的最小可能值;否则,输出 “Greed is good”(不带引号)。
输入输出样例
输入#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.
在第一个样例中,Bob 应当给 Alfred 一枚面值为 5 的硬币。这将形成题目描述中的情形。
在第二个样例中,不存在任何一组硬币会使 Alfred 的算法失效。
输入解题思路,AI测评打分。不知道怎么写?