思路在这里
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
constexpr int N = 100010;
int n, sticks[N], sum, length;
bool st[N];
bool dfs(int u, int cur, int start){
if (u * length == sum)
return true;
if (cur == length)
return dfs(u + 1, 0, 0);
for (int i = start; i < n; i ++){
if (st[i]) continue;
int l = sticks[i];
if (cur + l > length)
continue;
st[i] = true;
if (dfs(u, cur + l, i + 1))
return true;
st[i] = false;
if (!cur) return false;
if (cur + l == length) return false;
int j = i;
while (j < n && sticks[j] == l)
j ++;
i = j - 1;
}
return false;
}
int main(){
cin >> n;
sum = 0, length = 0;
for (int i = 0; i < n; i ++){
int l;
cin >> l;
sticks[i] = l;
sum += l;
length = max(l, length);
}
sort(sticks, sticks + n);
reverse(sticks, sticks + n);
memset(st, false, sizeof st);
while (length){
if (sum % length == 0 && dfs(0, 0, 0)){
cout << length << endl;
break;
}
length ++;
}
return 0;
}