分析
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6be6af9654fcd763010a08c00293cd47.png)
代码
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N=70;
bool st[N];
int n;
int w[N];
int len;
int sum;
bool dfs(int u,int cnt,int start){
if(u*len==sum)return true;
if(cnt==len)return dfs(u+1,0,0);
for(int i=start;i<n;i++){
if(st[i])continue;
if(w[i]+cnt>len)continue;
st[i]=true;
if(dfs(u,cnt+w[i],i+1))return true;
st[i]=false;
if(!cnt||cnt+w[i]==len)return false;
int j=i;
while(j<n&&w[j]==w[i])j++;
i=j-1;
}
return false;
}
int main(){
while(cin >> n,n){
memset(st,false,sizeof st);
sum=0;
len=0;
for(int i=0;i<n;i++){
cin >> w[i];
sum+=w[i];
}
sort(w, w + n);
reverse(w, w + n);
len=w[0];
while(len){
if(sum % len == 0&&dfs(0,0,0)){
cout << len <<"\n";
break;
}
len++;
}
}
}