https://www.luogu.com.cn/record/138164217
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
bool used[70], found = 0;
int n, a[70], maxn = 0, sum = 0, len, num, tmp;
int cmp(const int &x, const int &y) {return x > y;}
void dfs(int dep, int last, int rest) {
if(dep == num) {
found = 1;
return ;
}
if(rest == 0) rep(i, n) if(!used[i]) {
used[i] = 1;
dfs(dep + 1, i, len - a[i]);
break;
}
for(int i = last + 1; i < n; i++) if(!used[i] && rest >= a[i]) {
used[i] = 1;
dfs(dep, i, rest - a[i]);
used[i] = 0;
tmp = i;
while(i < n - 1 && a[i] == a[tmp]) i++;
if(i == n - 1) return ;
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
rep(i, n) {
cin >> a[i];
maxn = max(maxn, a[i]);
sum += a[i];
}
sort(a, a + n, cmp);
for(int i = maxn; i <= sum; i++) if(sum % i == 0) {
memset(used, 0, sizeof(used));
len = i, used[0] = 1, found = 0, num = sum / len;
dfs(1, 0, len - a[0]);
if(found) {
cout << len;
return 0;
}
}
}