[ARC085E] MUL 题解
设 solve(S) 为只考虑 solve 解决,感性理解一下可以发现在较小的数都被决策完之后图上的连通块不可能十分大,这种做法在
#include <bits/stdc++.h>
#define N 110
#define ll long long
using namespace std;
int n, val[N], vis[N];
vector<int> v[N];
ll solve(vector<int> c) {
if (c.empty())
return 0;
for (auto x : c)
vis[x] = 1;
queue<int> q;
vis[c.front()] = 0, q.push(c.front());
while (!q.empty()) {
int x = q.front();
q.pop();
for (auto to : v[x]) {
if (vis[to]) {
vis[to] = 0;
q.push(to);
}
}
}
vector<int> a, b;
for (auto x : c) {
if (!vis[x]) {
if (x != c.front())
a.push_back(x);
} else
vis[x] = 0, b.push_back(x);
}
vector<int> tmp;
for (auto x : a)
if (x % c.front())
tmp.push_back(x);
return solve(b) + max(solve(tmp), solve(a) + val[c.front()]);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> val[i];
for (int i = 1; i <= n; i++)
for (int j = 2; i * j <= n; j++)
v[i].push_back(i * j), v[i * j].push_back(i);
vector<int> c;
for (int i = 1; i <= n; i++)
c.push_back(i);
cout << solve(c) << '\n';
return 0;
}