#include <bits/stdc++.h>
using namespace std;
int a[100001];
set<pair<int, pair<int, int>>> edges;
int cmp[100001];
int find(int A) {
while (A != cmp[A]) cmp[A] = cmp[cmp[A]], A = cmp[A];
return A;
}
void onion(int A, int B) {
cmp[find(A)] = cmp[find(B)];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
set<int> s;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
s.insert(x);
}
n = 0;
for (int i : s) a[n++] = i;
for (int i = 0; i < n; i++) {
if (i != n - 1) edges.insert({a[i + 1] - a[i], {i, i + 1}});
int lb = i + 1;
for (int j = 2 * a[i]; j <= a[n - 1]; j += a[i]) {
while (a[lb] < j) lb++;
edges.insert({a[lb] - j, {i, lb}});
}
}
for (int i = 0; i < n; i++) cmp[i] = i;
long long ans = 0;
for (pair<int, pair<int, int>> e : edges) {
if (find(e.second.first) != find(e.second.second)) {
ans += e.first;
onion(e.second.first, e.second.second);
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
888 KB |
Output is correct |
2 |
Execution timed out |
5104 ms |
173800 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
888 KB |
Output is correct |
2 |
Execution timed out |
5086 ms |
620804 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
760 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
6 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2314 ms |
72532 KB |
Output is correct |
2 |
Correct |
4787 ms |
268100 KB |
Output is correct |
3 |
Correct |
2817 ms |
142048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
12580 KB |
Output is correct |
2 |
Correct |
4035 ms |
141872 KB |
Output is correct |
3 |
Correct |
1754 ms |
72740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3460 ms |
152084 KB |
Output is correct |
2 |
Execution timed out |
5048 ms |
373768 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
436 ms |
25712 KB |
Output is correct |
2 |
Execution timed out |
5079 ms |
343560 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3290 ms |
90400 KB |
Output is correct |
2 |
Execution timed out |
5077 ms |
344940 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4014 ms |
122648 KB |
Output is correct |
2 |
Execution timed out |
5115 ms |
319732 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
258 ms |
13364 KB |
Output is correct |
2 |
Execution timed out |
5070 ms |
241272 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |