28 30 tle,求更改
查看原帖
28 30 tle,求更改
1259871
jkZJM110211楼主2025/7/21 10:18
#include<bits/stdc++.h>
using namespace std;
int n;
vector<int> a;
int sum;
int max_len;
bool dfs(int id, int sum, int num, int cnt, vector<bool>& used) {
    if (cnt == n) {
        return true;
    }
    if (sum == num) {
        return dfs(0, 0, num, cnt, used);
    }
    for (int i = id; i < n; ++i) {
        if (!used[i] && sum + a[i] <= num) {
            used[i] = true;
            if (dfs(i + 1, sum + a[i], num, cnt + 1, used)) {
                return true;
            }
            used[i] = false;
            if (sum == 0 || sum + a[i] == num) {
                break;
            }
            while (i + 1 < n && a[i] == a[i + 1]) {
                ++i;
            }
        }
    }
    return false;
}

bool check(int L) {
    int k = sum / L;
    vector<bool> used(n, false);
    return dfs(0, 0, L, 0, used);
}
bool cmp(int x,int y){
	return x>y;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    a.resize(n);
    int max_len = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        max_len = max(max_len, a[i]);
    }
    sum = accumulate(a.begin(), a.end(), 0);
    sort(a.begin(), a.end(),cmp);

    for (int L = max_len; L <= sum; ++L) {
        if (sum % L != 0) {
            continue;
        }
        if (check(L)) {
            cout<< L << endl;
            return 0;
        }
    }

    cout << sum;
    return 0;
}
2025/7/21 10:18
加载中...