Canada Cup 2016 E. Too Much Money(贪心)

来源:互联网 发布:博采网络推广怎么做 编辑:程序博客网 时间:2024/06/04 19:02

Alfred wants to buy a toy moose that costs c dollars. The store doesn’t give change, so he must give the store exactlyc dollars, no more and no less. He has n coins. To make c dollars from his coins, he follows the following algorithm: letS 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 inS 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 fromS 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 exactlyc. 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 toS and then give up, since adding any other coin would cause the value of the coins inS 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 containsn (1 ≤ n ≤ 200 000) — the number of coins Alfred initially has. Thenn lines follow, each containing a single integerx (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).

Examples
Input
123534
Output
5
Input
508124816373737
Output
Greed is good


题意:一堆硬币每次贪心取最大问能否凑成C,现在已经保证了当前的n个硬币可以凑出c,问最少添加价值多少的硬币使得凑不出C。


分析:首先,不管添加多少硬币都可以转化为添加一枚的情况,然后我们就可以暴力枚举它的面额,然后每次最坏√n的判断。


#include <cstdio>#include <iostream>#include <map>using namespace std;map <int,int> f;int n,c,x;bool deal(int x){int temp = x;while(x){auto p = f.upper_bound(temp);if(p == f.begin()) return false;p--;x -= min((x / p->first)*(p->first),p->first * (p->second));temp = min(x,p->first-1);}return true;}int main(){scanf("%d",&c);scanf("%d",&n);for(int i = 1;i <= n;i++) {scanf("%d",&x);f[x]++;}for(int i = 1;i < c;i++){f[i]++;if(!deal(c)){printf("%d",i);return 0;}if(f[i] == 1) f.erase(i);else f[i]--;}printf("Greed is good");return 0;}


0 0