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

65 篇文章 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.

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


题意:一堆硬币每次贪心取最大问能否凑成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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值