#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <tuple>
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;
for (int c : q) {
p.push_back(c);
}
n = p.size();
vector<tuple<int, int, int>> edges;
for (int j = 0; j < n; j++) {
int c = p[j];
auto it1 = upper_bound(p.begin(), p.end(), c);
edges.push_back(make_tuple(*it1 % c, j, it1 - p.begin()));
for (int i = c; i <= mp; i += c) {
auto it = lower_bound(p.begin(), p.end(), i);
edges.push_back(make_tuple(*it % c, j, it - p.begin()));
}
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
248 ms |
30264 KB |
Output is correct |
2 |
Incorrect |
796 ms |
104172 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
35 ms |
6888 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
480 ms |
54812 KB |
Output is correct |
2 |
Runtime error |
1081 ms |
154012 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
9792 KB |
Output is correct |
2 |
Correct |
991 ms |
103664 KB |
Output is correct |
3 |
Incorrect |
334 ms |
54808 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
293 ms |
30476 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
336 ms |
58776 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
42 ms |
7872 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |