0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-09 17:18:49 +00:00

P1120 小木棍

https://www.luogu.com.cn/record/85778864
This commit is contained in:
Baoshuo Ren 2022-09-03 17:08:35 +08:00
parent 5c7ea9cfd8
commit 2f7eb7552b
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

76
Luogu/P1120/P1120.cpp Normal file
View File

@ -0,0 +1,76 @@
#include <iostream>
#include <algorithm>
#include <functional>
#include <numeric>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 70;
int n, m, l, a[N], next[N];
bool vis[N]{false, true};
void dfs(int now, int last, int rest) {
if (!rest) {
if (now == m) {
cout << l << endl;
exit(0);
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
vis[i] = true;
dfs(now + 1, i, l - a[i]);
vis[i] = false;
break;
}
}
}
int p = std::lower_bound(a + last + 1, a + n + 1, rest, std::greater<int>()) - a;
for (int i = p; i <= n; i++) {
if (!vis[i]) {
vis[i] = true;
dfs(now, i, rest - a[i]);
vis[i] = false;
if (rest == a[i] || rest == l) return;
i = next[i];
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int sum = std::accumulate(a + 1, a + 1 + n, 0);
std::sort(a + 1, a + 1 + n, std::greater<int>());
for (int i = n; i; i--) {
next[i] = a[i] == a[i + 1] ? next[i + 1] : i;
}
for (l = a[1]; l <= sum / 2; l++) {
if (sum % l) continue;
m = sum / l;
dfs(1, 1, l - a[1]);
}
cout << sum << endl;
return 0;
}