E. Too Much Money

Codeforces
IDCF725E
Time2000ms
Memory256MB
Difficulty
brute forcegreedy
English · Original
Chinese · Translation
Formal · Original
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. ## Input 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. ## Output If there is a solution, print the minimum possible total value of the coins in a solution. Otherwise, print "_Greed is good_" (without quotes). [samples] ## Note 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.
Alfred 想要买一个售价为 $c$ 美元的玩具驼鹿。商店不找零,因此他必须恰好支付 $c$ 美元,不多不少。他有 $n$ 枚硬币。为了用他的硬币凑出 $c$ 美元,他遵循以下算法:设 $S$ 为正在使用的硬币集合,初始时 $S$ 为空。Alfred 反复向 $S$ 中添加他拥有的最大面值硬币,使得添加该硬币后 $S$ 中硬币的总价值不超过 $c$。如果不存在这样的硬币,且 $S$ 中硬币的总价值仍小于 $c$,他就放弃并回家。注意,Alfred 在将硬币加入 $S$ 后从不移除它。 作为程序员,你可能知道,即使存在一组硬币恰好能凑出 $c$ 美元,Alfred 的算法仍可能失败。例如,如果 Alfred 有一枚价值 $3$ 美元的硬币、一枚价值 $4$ 美元的硬币和两枚价值 $5$ 美元的硬币,而驼鹿售价为 $12$ 美元,则 Alfred 会将两枚 $5$ 美元的硬币加入 $S$,然后放弃,因为再添加任何其他硬币都会使 $S$ 的总价值超过 $12$。当然,Alfred 本可以组合一枚 $3$ 美元、一枚 $4$ 美元和一枚 $5$ 美元的硬币来达到总和。 Bob 曾试图说服 Alfred 他的算法有缺陷,但 Alfred 不相信他。现在 Bob 希望给 Alfred 一些硬币(除了 Alfred 已有的硬币外),使得 Alfred 的算法失败。Bob 可以给 Alfred 任意数量、任意面值的硬币(前提是每枚硬币的价值必须为正整数美元)。允许存在多个相同面值的硬币。他希望最小化他给 Alfred 的硬币的总价值。请找出这个最小值。如果无解,请输出 "_Greed is good_"。你可以假设,如果存在解,则其值为正数。换句话说,如果 Bob 不给 Alfred 任何硬币,Alfred 的算法会成功。 第一行包含 $c$ ($1 ≤ c ≤ 200 000$) —— Alfred 想支付的价格。第二行包含 $n$ ($1 ≤ n ≤ 200 000$) —— Alfred 最初拥有的硬币数量。接下来 $n$ 行,每行包含一个整数 $x$ ($1 ≤ x ≤ c$),表示 Alfred 的一枚硬币的价值。 ## Input 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. ## Output If there is a solution, print the minimum possible total value of the coins in a solution. Otherwise, print "_Greed is good_" (without quotes). [samples] ## Note 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.
**Definitions** Let $ c \in \mathbb{Z}^+ $ be the target cost. Let $ A = \{a_1, a_2, \dots, a_n\} \subseteq \mathbb{Z}^+ $ be the multiset of Alfred’s initial coins, with $ a_i \leq c $. Let $ G $ be the greedy algorithm: repeatedly select the largest coin $ x \in A $ such that $ \text{sum}(S) + x \leq c $, adding $ x $ to set $ S $, until no such coin exists. **Constraints** 1. $ 1 \leq c \leq 200{,}000 $ 2. $ 1 \leq n \leq 200{,}000 $ 3. $ 1 \leq a_i \leq c $ for all $ a_i \in A $ 4. The greedy algorithm succeeds with $ A $ alone (i.e., $ \text{sum}(S_{\text{greedy}}) = c $ or fails but no subset of $ A $ sums to $ c $ — but problem states algorithm works initially). **Objective** Find the minimum total value $ v \in \mathbb{Z}^+ $ of a multiset $ B \subseteq \mathbb{Z}^+ $ such that: - $ A' = A \cup B $ - The greedy algorithm on $ A' $ fails to reach exactly $ c $ (i.e., terminates with $ \text{sum}(S) < c $) - There exists *some* subset of $ A' $ that sums to exactly $ c $ If no such $ B $ exists, output "_Greed is good_".
Samples
Input #1
12
3
5
3
4
Output #1
5
Input #2
50
8
1
2
4
8
16
37
37
37
Output #2
Greed is good
API Response (JSON)
{
  "problem": {
    "name": "E. Too Much Money",
    "description": {
      "content": "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 hi",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF725E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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 hi...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Alfred 想要买一个售价为 $c$ 美元的玩具驼鹿。商店不找零,因此他必须恰好支付 $c$ 美元,不多不少。他有 $n$ 枚硬币。为了用他的硬币凑出 $c$ 美元,他遵循以下算法:设 $S$ 为正在使用的硬币集合,初始时 $S$ 为空。Alfred 反复向 $S$ 中添加他拥有的最大面值硬币,使得添加该硬币后 $S$ 中硬币的总价值不超过 $c$。如果不存在这样的硬币,且 $S$ 中硬币的总价...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ c \\in \\mathbb{Z}^+ $ be the target cost.  \nLet $ A = \\{a_1, a_2, \\dots, a_n\\} \\subseteq \\mathbb{Z}^+ $ be the multiset of Alfred’s initial coins, with $ a_i \\leq c $.  \nLet $ G...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments