#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <cstring>
#include <sstream>
#include <map>
#include <stack>
#include <queue>
#include <ctime>
#include <cstdlib>
using namespace std;
int sticks[64];
bool over[64];
int sv;
//回溯法的dfs,深搜, 我称为dfs树(深搜树);
//假设成立原则, 及枚举其所有可能的最小值,由最小值回溯出组成最小值的过程,从而证明这个最小值是正确的。
bool dfs(int n,int m,int L,int pos){
if(n == 0 && m == 0)
return true;
if(m == 0)
{
m = L;
pos = -1;
}
for(int i = pos + 1; i < sv; i++){
if(!over[i] && sticks[i] <= m){
//剪枝1:相同长度的木棍不应该在相同位置重复出现;
if(!(i != 0 && over[i - 1] == false && sticks[i - 1] == sticks[i])){
over[i] = true;
if(dfs(n - 1, m - sticks[i], L, i)) return true;
else {
over[i] = false;
//剪枝思想: 运用假设成立,去证明剪枝的正确性
//剪枝2:当前木棍由许多小木棍组成;第i个小木棍因为后序的小木棍不能组成当前木棍而拆除时,i == 1时直接进行拆除上一根木棍;
//剪枝3: 当拆除的小木棍为木棍的最后一根木棍时,拆除是无意义的,应该直接拆倒数第二根小木棍(木棍数大于2)
if(m == L || m - sticks[i] == 0)
return false;
}
}
}
}
return false;
}
int main(){
int n;
int totalLen = 0;
while(cin >> n && n){
sv = n;
totalLen = 0;
for(int i = 0; i < n; i++){
cin >> sticks[i];
totalLen += sticks[i];
}
//剪枝4: 选取的下一根小木棍比前一根小木棍要大(小);
sort(sticks,sticks + n,greater<int>());
int L = 1;
for(; L <= (totalLen/2 + 1); L++){
memset(over,0,sizeof(over));
if(totalLen % L == 0){
if(dfs(n,L,L,-1)){
cout << L << endl;
break;
}
}
}
if(L >= (totalLen/2 + 1)){
cout << totalLen << endl;
}
}
}
12-22
1415
11-16
408