【P1120 小木棍】

代码是87分,说明问题大得很。这是我的思路,不知哪错了:
由于初始木棍要相等,所以可以对每一个可能长度进行判断,大到小排序,dfs判断能否将木棍全部用上。

#include <iostream>
#include <stdio.h>
#include <queue>
#include <set>
#include <string>
#include <map>
#include <cmath>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
const int maxn = 6e5 + 5;
const int inf = 0x3f3f3f3f;
int a[65], ju, n, all, vis[65];
int dfs(int st, int x, int now, int num)
{
    if (num == n)
        return 1;
    if (now == x)
    {
        if (dfs(1, x, 0, num))
            return 1;
    }
    for (int i = st; i <= n; ++i)
    {
        if (vis[i] == 0 && now + a[i] <= x)
        {
            vis[i] = 1;
            if (dfs(i + 1, x, now + a[i], num+1))
                return 1;
            vis[i] = 0;
            if (a[i] == x - now || now == 0)
                break;
            while (a[i] == a[i + 1])
                i++;
        }
    }
    return 0;
}
bool cmp(int a, int b)
{
    return a > b;
}
int main()
{
    all = 0;
    memset(vis, 0, sizeof(vis));
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &a[i]), all += a[i];
    sort(a + 1, a + n + 1, cmp);
    for (int i = a[1]; i <= all; ++i)
        if (all % i == 0 && dfs(1, i, 0, 0))
        {
            printf("%d\n", i);
            return 0;
        }
    return 0;
}