#include <bits/stdc++.h>
using namespace std;
int a[100001];
pair<int, pair<int, int>> edges[100000001];
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;
int cnt = 0;
for (int i = 0; i < n; i++) {
if (i != n - 1) edges[cnt++] = {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++;
pair<int, pair<int, int>> nxt = {a[lb] % a[i], {i, lb}};
if (edges[cnt - 1] != nxt) edges[cnt++] = nxt;
}
}
sort(edges, edges + cnt);
for (int i = 0; i < n; i++) cmp[i] = i;
long long ans = 0;
for (int i = 0; i < cnt; i++) {
pair<int, int> verts = edges[i].second;
if (find(verts.first) != find(verts.second)) {
ans += edges[i].first;
onion(verts.first, verts.second);
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
48 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
504 KB |
Output is correct |
2 |
Correct |
271 ms |
1124 KB |
Output is correct |
3 |
Correct |
5 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1581 ms |
17612 KB |
Output is correct |
2 |
Correct |
2029 ms |
50596 KB |
Output is correct |
3 |
Correct |
1251 ms |
21960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
2728 KB |
Output is correct |
2 |
Correct |
412 ms |
25744 KB |
Output is correct |
3 |
Correct |
1175 ms |
17016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1903 ms |
32616 KB |
Output is correct |
2 |
Correct |
2365 ms |
63616 KB |
Output is correct |
3 |
Correct |
1588 ms |
21956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
260 ms |
6392 KB |
Output is correct |
2 |
Correct |
2163 ms |
59844 KB |
Output is correct |
3 |
Correct |
1328 ms |
20732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1734 ms |
20040 KB |
Output is correct |
2 |
Execution timed out |
5091 ms |
273256 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1770 ms |
20116 KB |
Output is correct |
2 |
Execution timed out |
5032 ms |
377128 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
3624 KB |
Output is correct |
2 |
Execution timed out |
5060 ms |
374776 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |