Too Much Money

Too Much Money
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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).

Examples
input
12
3
5
3
4
output
5
input
50
8
1
2
4
8
16
37
37
37
output
Greed is good
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.

分析:从小到大枚举答案,然后模拟这个贪心过程,如果不能达到c,则输出答案;

   在贪心过程中注意优化,now-=max(1,min(now/(*a),num[*a]))*(*a);

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define freopen freopen("in.txt","r",stdin)
const int maxn=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,c,num[maxn];
set<int>p,tmp;
int main()
{
int i,j;
scanf("%d%d",&c,&n);
rep(i,,n)scanf("%d",&j),p.insert(j),num[j]++;
rep(i,,c)
{
int now=c;
tmp.clear();
p.insert(i);
num[i]++;
while(now>)
{
if(p.empty())return *printf("%d\n",i);
auto a=p.lower_bound(now);
if(a==p.end()||(*a>now&&a!=p.begin()))a--;
tmp.insert(*a);
now-=max(,min(now/(*a),num[*a]))*(*a);
p.erase(*a);
}
if(now<)return *printf("%d\n",i);
for(int x:tmp)p.insert(x);
if(--num[i]==)p.erase(i);
}
puts("Greed is good");
//system("Pause");
return ;
}

随机推荐

  1. C语言 百炼成钢16

    //题目46:海滩上有一堆桃子,五只猴子来分.第一只猴子把这堆桃子凭据分为五份,多了一个,这只 //猴子把多的一个扔入海中,拿走了一份.第二只猴子把剩下的桃子又平均分成五份,又多了 //一个,它同样把 ...

  2. 《剑指Offer》之二维数组中的查找

    1.题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 2.代码实现 pu ...

  3. &lbrack;转&rsqb; error LNK2026&colon; 模块对于 SAFESEH 映像是不安全的

    原文 今天使用VS2012遇到一个问题:"链接器工具错误 LNK2026 XXX模块对于 SAFESEH 映像是不安全的" 解决方法: 1.打开该项目的“属性页”对话框. 2.单击 ...

  4. C&num;监听文件

    //全局变量 public static FileSystemWatcher Watcher; /// <summary>        /// 设置监听配置        /// &lt ...

  5. ThinkPHP数据字段的缓存问题

    在使用ThinkPHP开发应用时,在项目运行后会在应用(Application)目录下产生一个Runtime缓存文件夹 在生成Runtime之后,若改变数据表的字段结构,则在PHP代码中无法为新的字段 ...

  6. &lbrack;ios 开发笔记&rsqb;:一句话笔记

    1.NSString转int int a=[@"123" intValue]; 同样适用于NSDictionary将NSNumber转为int   2.switch(stateme ...

  7. 设置IE浏览器跨域访问数据

    在开发中,经常会遇到多站点跨域访问后台服务获取数据的情况,解决方法有两种 自己写代理服务,访问代理服务,代理服务请求服务获取数据再返回: 设置浏览器可以跨域访问数据. 本文来讲如何设置IE浏览器跨域访 ...

  8. MariaDB&&num;160&semi;MySQL变量取值避免四舍五入的方法

    MySQL变量取值避免四舍五入的方法 By:授客 QQ:1033553122 在一些对数据精确度要求比较高的场景(比如资金结算)下,变量取值时不能对变量值进行四舍五入操作,这时候就要做些预处理工作. ...

  9. 快讯:微软安卓版个人助理(Cortana)在美国境内进行公測

    8月24日,"Microsoft starts public test of Cortana app for Android smartphones"(此文8月24日发表),此事意 ...

  10. Asp&period;Net Core MVC框架内置过滤器

    第一部分.MVC框架内置过滤器 下图展示了Asp.Net Core MVC框架默认实现的过滤器的执行顺序: Authorization Filters:身份验证过滤器,处在整个过滤器通道的最顶层.对应 ...

上一篇:Oracle 常用Sql 语句


下一篇:C# Dictionary字典类介绍