#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <tuple>
#include <queue>
using namespace std;
using ll = long long;
int n;
vector<int> parent, sizes;
int cc;
void init() {
parent.resize(n);
sizes.resize(n);
cc = n;
for (int i = 0; i < n; i++) {
parent[i] = i;
sizes[i] = 1;
}
}
int find_set(int a) {
if (parent[a] == a) {
return a;
}
return parent[a] = find_set(parent[a]);
}
void join_set(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a == b) {
return;
}
if (sizes[a] > sizes[b]) {
swap(a, b);
}
cc--;
parent[a] = b;
sizes[b] += sizes[a];
}
int main() {
cin >> n;
set<int> q;
int mp = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
q.insert(x);
mp = max(mp, x);
}
vector<int> p;
int cur = 0;
vector<int> nextL(mp + 1);
int curIt = 0;
for (int c : q) {
p.push_back(c);
for (cur; cur <= c; cur++) {
nextL[cur] = curIt;
}
curIt++;
}
n = p.size();
// Kruskal's Algorithm for MST
vector<tuple<int, int, int>> edges;
for (int j = 0; j < n; j++) {
int c = p[j];
if (j != n - 1) {
edges.push_back(make_tuple(p[j + 1] % c, j, j + 1));
}
for (int i = 2; i <= mp / c; i ++) {
edges.push_back(make_tuple(p[nextL[i * c]] % c, j, nextL[i * c]));
}
}
sort(edges.begin(), edges.end());
ll ans = 0;
init();
for (auto c : edges) {
ll w, a, b;
tie(w, a, b) = c;
if (find_set(a) == find_set(b)) {
continue;
}
join_set(a, b);
ans += w;
if (cc == 1) {
break;
}
}
cout << ans << endl;
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:71:8: warning: statement has no effect [-Wunused-value]
71 | for (cur; cur <= c; cur++) {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
39512 KB |
Output is correct |
2 |
Correct |
228 ms |
88756 KB |
Output is correct |
3 |
Correct |
24 ms |
39956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Runtime error |
566 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
39616 KB |
Output is correct |
2 |
Correct |
22 ms |
39556 KB |
Output is correct |
3 |
Correct |
23 ms |
39880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
183 ms |
34108 KB |
Output is correct |
2 |
Correct |
566 ms |
58576 KB |
Output is correct |
3 |
Correct |
278 ms |
57864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
8160 KB |
Output is correct |
2 |
Correct |
307 ms |
54204 KB |
Output is correct |
3 |
Correct |
172 ms |
31296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
362 ms |
58664 KB |
Output is correct |
2 |
Correct |
767 ms |
107804 KB |
Output is correct |
3 |
Correct |
242 ms |
33868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
9288 KB |
Output is correct |
2 |
Correct |
759 ms |
106892 KB |
Output is correct |
3 |
Correct |
242 ms |
33552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
244 ms |
69432 KB |
Output is correct |
2 |
Runtime error |
677 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
275 ms |
69492 KB |
Output is correct |
2 |
Runtime error |
679 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
43724 KB |
Output is correct |
2 |
Runtime error |
768 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |