2019.01.26【NOIP提高组】模拟B组 JZOJ 1252 天平

版权声明:虽然本蒟蒻很菜,但各位dalao转载请注明出处谢谢。 https://blog.csdn.net/xuxiayang/article/details/86656974

D e s c r i p i o n Descripion

n n 个砝码,每个砝码的重量为 a i a_i ,求在总重量不超过 C C 的前提下可称出的最大重量


S o l u t i o n Solution

暴搜睿智题


C o d e Code

#include<cstdio>
#include<algorithm>
using namespace std;int n,x;
long long ans,s[1001],a[1001],c;
inline void dfs(int dep,long long now)
{
	if(now>c) return;
	if(s[dep-1]+now<=c)//一个小剪枝
	{
		ans=max(ans,s[dep-1]+now);
		return;
	}
	ans=max(ans,now);
	for(register int i=1;i<dep;i++) dfs(i,now+a[i]);
	return;
}
signed main()
{
	scanf("%d%lld",&n,&c);
	for(register int i=1;i<=n;i++) scanf("%d",a+i),s[i]=s[i-1]+a[i];
	dfs(n+1,0);
	printf("%lld\n",ans);
}

猜你喜欢

转载自blog.csdn.net/xuxiayang/article/details/86656974