1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include <iostream> #include <cstdio> #include <cstring> #include <algorithm>
using namespace std;
int d[70],used[70],len,n,sum,ok;
bool cmp(int a,int b) { return a>b; }
void dfs(int a,int b,int c) { if(ok==1) return; else if(a==n+1) return; else if(c>sum||b>len) return; else if(c==sum&&b;==len) ok=1; else if(b==len) dfs(0,0,c); else if(used[a+1]==1) dfs(a+1,b,c); else { used[a+1]=1; dfs(a+1,b+d[a+1],c+d[a+1]); used[a+1]=0; dfs(a+1,b,c); } }
int main() { int i; while(cin>>n&&n;) { sum=0;ok=0; d[0]=0; for(i=1;i<=n;i++) { cin>>d[i]; sum+=d[i]; } sort(d+1,d+1+n,cmp); for(i=d[1];i<=sum;i++) { if(ok==1) break; if(sum%i==0) { memset(used,0,sizeof(used)); len=i; dfs(0,0,0); } } cout<<len<<endl; } }
|