RT,这里
之前两次捞都没得到结果,大概是因为dalao们都不在线/kk。好吧,求大佬们帮我看看:
#include <bits/stdc++.h>
using namespace std;
int a[105],used[105],n,m,len,minn=0,sum=0,bj,cnt;
inline bool cmp(int a, int b) {
return a>b;
}
inline int read() {
char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
int x=0;
while(ch>='0'&&ch<='9') x=x*10+(ch^48),ch=getchar();
return x;
}
inline int max(int x, int y) {
return x>y?x:y;
}
inline void dfs(int k, int last, int rest) {
int i,j;
if(!(k^m)) {
bj=1;
return;
}
if(!rest) {
for(i=1;i<=n;i++)
if(!used[i]) {
*(used+i)=1;
break;
}
dfs(k+1,i,len-a[i]);
}
for(i=last+1;i<=n;i++)
if(!used[i]&&rest>=a[i]) {
*(used+i)=1;
dfs(k,i,rest-a[i]);
*(used+i)=0,j=i;
while(i<n&& !(a[i]^a[j])) i++;
if(!(i^n)) return;
}
}
int main(void) {
scanf("%d",&n);
int t;
for(int i=1;i<=n;i++) {
t=read();
if(t>50) continue;
a[++cnt]=t;
minn=max(minn,a[i]);
sum+=t;
}
sort(a+1,a+cnt+1,cmp);
int i,j;
for(i=minn;i<=sum;i++)
if(sum%i==0) {
memset(used,0,sizeof(used));
len=i,used[1]=1,bj=0,m=sum/i;
dfs(1,1,len-a[1]);
if(bj) {
printf("%d\n",len);
break;
}
}
return 0;
}