#include <bits/stdc++.h>
#define index ind
using namespace std;
struct DSU {
int cnt;
int size;
vector<int> e;
DSU (int _) : size (_) {
e = vector<int> (size, -1);
}
int root(int x) {
return (e[x] < 0 ? x : (e[x] = root(e[x])));
}
bool same(int x, int y) {
return root(x) == root(y);
}
void unite(int x, int y) {
if (same(x, y)) return;
x = root(x), y = root(y);
if (-e[x] > -e[y]) {
swap(x, y);
}
e[y] += e[x];
e[x] = y;
cnt --;
}
};
const int N = (int)1e5;
const int Mx = (int)1e7;
int n, p[N];
int nxt[Mx + 1];
int index[Mx + 1];
vector<int> values;
vector<pair<int, int>> edges[Mx + 1];
vector<tuple<int, int, int>> sr_edges;
void solve() {
cin >> n;
for (int i = 0; i < n;i ++) {
cin >> p[i];
}
sort(p, p + n);
values.push_back(p[0]);
for (int i = 1; i < n;i ++) {
if (p[i] != p[i - 1]) {
values.push_back(p[i]);
}
}
int ptr = 0;
for (auto& e : values) {
nxt[e] = 1;
index[e] = ptr;
ptr ++;
}
int crr = -1;
for (int i = Mx; i > 0; i --) {
int prev = crr;
if (nxt[i] == 1) crr = i;
nxt[i] = prev;
}
for (auto& e : values) {
for (int i = e; i <= Mx; i += e) {
if (i != e && index[i] != -1) {
edges[0].push_back(make_pair(index[e], index[i]));
} else if (nxt[i] != -1) {
edges[nxt[i] - i].push_back(make_pair(index[e], index[nxt[i]]));
}
}
}
for (int i = 0; i < Mx + 1; i ++) {
for (auto& e : edges[i]) {
sr_edges.push_back(make_tuple(e.first, e.second, i));
}
}
DSU d(n);
int64_t ans = 0;
for (auto& e : sr_edges) {
int x, y, w; tie(x, y, w) = e;
// cout << x + 1 << ' ' << y + 1 << ' ' << w << '\n';
if (!d.same(x, y)) {
d.unite(x, y);
ans += w;
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
for (int i = 0; i < Mx + 1; i ++) {
index[i] = -1;
}
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
313664 KB |
Output is correct |
2 |
Correct |
441 ms |
395600 KB |
Output is correct |
3 |
Correct |
165 ms |
314376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
240 ms |
313716 KB |
Output is correct |
2 |
Runtime error |
1819 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
313716 KB |
Output is correct |
2 |
Correct |
156 ms |
313508 KB |
Output is correct |
3 |
Correct |
171 ms |
313932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
375 ms |
349872 KB |
Output is correct |
2 |
Correct |
886 ms |
404820 KB |
Output is correct |
3 |
Correct |
453 ms |
385820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
198 ms |
318872 KB |
Output is correct |
2 |
Correct |
754 ms |
387480 KB |
Output is correct |
3 |
Correct |
492 ms |
350596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
661 ms |
386360 KB |
Output is correct |
2 |
Correct |
1092 ms |
470608 KB |
Output is correct |
3 |
Correct |
475 ms |
358088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
399 ms |
323680 KB |
Output is correct |
2 |
Correct |
1187 ms |
474892 KB |
Output is correct |
3 |
Correct |
425 ms |
360684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
321 ms |
353192 KB |
Output is correct |
2 |
Runtime error |
1750 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
313 ms |
358200 KB |
Output is correct |
2 |
Runtime error |
2865 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
191 ms |
319092 KB |
Output is correct |
2 |
Runtime error |
3820 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |