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 68 69 70 71 72 73 74 75
| #include<bits/stdc++.h>
using namespace std;
const int N = 70;
int n,m; int num, len, sum; int a[N]; bool st[N];
bool cmp(int a, int b) { return a > b; }
bool dfs(int k, int last, int s) {
if (k > num)return true;
if (s == len) return dfs(k + 1, 1, 0);
int fail = 0;
for (int i = last; i <= n; i++) { if (!st[i]&&a[i]+s<=len&&fail!=a[i]) { st[i] = true; if (dfs(k, i, s + a[i]))return true; st[i] = false;
fail = a[i];
if (s == 0 || s + a[i] == len)return false; } } return false; }
int main() { while (cin >> n && n) { memset(st, false, sizeof st); memset(a, 0, sizeof a); m = num = len = sum = 0;
for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; }
sort(a + 1, a + 1 + n, cmp);
for (len = a[1]; len <= sum / 2; len++) { if (sum % len)continue;
num = sum / len; if (dfs(1, 1, 0)) { m = 1; cout << len << endl; break; } } if(!m)cout << sum << endl; } return 0; }
|