搜索+背包就是了

#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;
}

相关文章:

  • 2021-07-22
  • 2022-01-12
  • 2021-06-29
  • 2022-03-04
  • 2022-12-23
猜你喜欢
  • 2021-08-11
  • 2021-06-01
  • 2021-11-04
相关资源
相似解决方案