33pts求救
查看原帖
33pts求救
667558
_Kamisato_Ayaka_楼主2023/7/12 16:28
#include <bits/stdc++.h>
using namespace std;

bool dfs(int tg,int idx,vector<int>& sticks,vector<bool>& vis){
	if(tg == 0)
		return idx == sticks.size();
	for(int i = idx;i < sticks.size();i ++) {
		if(sticks[i] > tg) return false;
		if(!vis[i] && sticks[i] <= tg){
			vis[i] = true;
			if(dfs(tg - sticks[i],i + 1,sticks,vis))
				return true;
			vis[i] = false;
		}
	}
	return false;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	std::cin >> n;
	vector<int>sticks(n + 1);
	for (int i = 1;i <= n;i ++)
		std::cin >> sticks[i];
	std::sort(sticks.begin(), sticks.end());
	int total_length = 0;
	for(int i = 1;i <= n;i ++)
		total_length += sticks[i];
	int min_length = total_length;
	for(int length = 1; length <= total_length / 2; length++){
		if(total_length % length == 0) {
			vector<bool>vis(n + 1,false);
			if(dfs(length,0,sticks,vis)){
				min_length = length;
				break;
			}
		}
	}
	std::cout << min_length << std::endl;
	return 0;
}
2023/7/12 16:28
加载中...