#include
<
stdio.h
>
#include < stdlib.h >
#include < memory.h >
int totLen,len,maxL,n,i,k;
int s[ 64 ];
int used[ 64 ];
int cmp( const void * a, const void * b) ... {
return (*(int *)b) - (*(int *)a);
}
bool Search( int times, int rest, int pos) ... {
int flag=0;
if(rest == len) flag = 1;
if(times==totLen/len)
return 1;
int i;//尽量用局部变量
for(i=pos;i<n;i++)...{
if(used[i])
continue;
if(s[i]==rest)...{
used[i]=1;
if(Search(times+1,len,0))
return 1;
used[i]=0;//大的刚好匹配的s[i]不能填入,不必再考虑更小的了.因若s[j]+s[k](j,k<i)
//填入前面的棍(s[j]+s[k]==s[i])、s[i]填入后面的棍可行,则s[j]+s[k]与s[i]调换肯定也ok
return 0;
}
else if(s[i]<rest)...{
used[i]=1;
if( Search(times,rest-s[i],i+1) )return 1;
used[i]=0;
if(flag) return 0; //因为rest==len的情况下(新棍开始),s[i]一定要
//被用上(s[i]是从0开始第一个未被用过的小棒),但找不到解,说明此时len不是解
while(s[i+1]==s[i])i++;
}
}
return 0;
}
int main()
... {
int len1;
while( scanf("%d",&n),n>0 )...{
memset(s,0,sizeof(s));
maxL=-1,totLen=0;
for(i=0;i<n;i++)...{
scanf("%d",&s[i]);
totLen+=s[i];
if(s[i]>maxL)
maxL=s[i];
}
qsort(s,n,sizeof(s[0]),cmp);
for(len1=maxL;len1<=totLen;len1++)...{
if(totLen%len1!=0)
continue;
len=len1;
memset(used,0,sizeof(used));
if(Search(1,len1,0))...{
printf("%d ",len1);
break;
}
}
}
return 1;
}
#include < stdlib.h >
#include < memory.h >
int totLen,len,maxL,n,i,k;
int s[ 64 ];
int used[ 64 ];
int cmp( const void * a, const void * b) ... {
return (*(int *)b) - (*(int *)a);
}
bool Search( int times, int rest, int pos) ... {
int flag=0;
if(rest == len) flag = 1;
if(times==totLen/len)
return 1;
int i;//尽量用局部变量
for(i=pos;i<n;i++)...{
if(used[i])
continue;
if(s[i]==rest)...{
used[i]=1;
if(Search(times+1,len,0))
return 1;
used[i]=0;//大的刚好匹配的s[i]不能填入,不必再考虑更小的了.因若s[j]+s[k](j,k<i)
//填入前面的棍(s[j]+s[k]==s[i])、s[i]填入后面的棍可行,则s[j]+s[k]与s[i]调换肯定也ok
return 0;
}
else if(s[i]<rest)...{
used[i]=1;
if( Search(times,rest-s[i],i+1) )return 1;
used[i]=0;
if(flag) return 0; //因为rest==len的情况下(新棍开始),s[i]一定要
//被用上(s[i]是从0开始第一个未被用过的小棒),但找不到解,说明此时len不是解
while(s[i+1]==s[i])i++;
}
}
return 0;
}
int main()
... {
int len1;
while( scanf("%d",&n),n>0 )...{
memset(s,0,sizeof(s));
maxL=-1,totLen=0;
for(i=0;i<n;i++)...{
scanf("%d",&s[i]);
totLen+=s[i];
if(s[i]>maxL)
maxL=s[i];
}
qsort(s,n,sizeof(s[0]),cmp);
for(len1=maxL;len1<=totLen;len1++)...{
if(totLen%len1!=0)
continue;
len=len1;
memset(used,0,sizeof(used));
if(Search(1,len1,0))...{
printf("%d ",len1);
break;
}
}
}
return 1;
}