#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[64];
int visit[64];
int length, n;
int cmp(int a, int b)
{
return a > b;
}
int dfs(int pos, int num, int need)
{
if (need == 0)//拼好了一个长度为length的木棍, 改变状态
{
need = length;
pos = 0;
}
if (num == n)//拼接成功:所有的木棍都用了
return 1;
for (int i = pos; i < n; i++)
{
if (visit[i] == 0 && need >= a[i])
{
visit[i] = 1;
if (dfs(i + 1, num + 1, need - a[i]))
return 1;
visit[i] = 0;
if (need == a[i])//如果大的拼接不成功,用小的来替换这个大的,是徒劳的
return 0;
if (need == length)//如果第一根木棍拼接不成功,则换掉第一根是没有意义的
return 0;
while (i + 1 < n && a[i + 1] == a[i])
i++;//如果拼接不成功,不用搜相同长度的木棍
}
}
return 0;
}
int main()
{
while (scanf("%d", &n) != EOF && n != 0)
{
memset(a, 0, sizeof(a));
memset(visit, 0, sizeof(visit));
int sum = 0;
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
sum += a[i];
}
sort(a, a + n, cmp);
length = a[0];
visit[0] = 1;
for (; length <= sum; length++)
{
if (sum % length != 0)
continue;
else
{
if (dfs(0, 1, length - a[0]))
break;
}
}
printf("%d\n", length);
}
return 0;
}
nyojSticks
最新推荐文章于 2018-04-21 14:26:27 发布