luogu1441 砝码称重

搜索+背包就是了

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n, m, a[25], ans=0, lst=0;
bool isu[25], f[2005];
void dfs(int x){
	if(x==m+1){
		int cnt=0;
		memset(f, 0, sizeof(f));
		f[0] = true;
		for(int i=1; i<=n; i++)
			if(!isu[i])
				for(int j=2000; j>=a[i]; j--)
					f[j] |= f[j-a[i]];
		for(int i=1; i<=2000; i++)
			if(f[i])
				cnt++;
		ans = max(ans, cnt);
		return ;
	}
	for(int i=lst+1; i<=n; i++){
		isu[i] = true;
		lst = i;
		dfs(x+1);
		isu[i] = false;
	}
}
bool vis[2005];
int main(){
	cin>>n>>m;
	for(int i=1; i<=n; i++)	scanf("%d", &a[i]);
	dfs(1);
	cout<<ans<<endl;
	return 0;
}
原文地址:https://www.cnblogs.com/poorpool/p/8067646.html