#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAX = 2E7;
const int MAXN = 1E5 + 5;
int par[MAXN];
struct DSU {
int n;
DSU(int n) : n(n) {
iota(par, par + n, 0);
}
int get(int a) {
return par[a] = (par[a] == a ? a : get(par[a]));
}
bool same(int a, int b) {
return get(a) == get(b);
}
bool unite(int a, int b) {
if(same(a, b)) {
return false;
}
a = get(a), b = get(b);
par[b] = a;
return true;
}
};
int cost(int a, int b) {
return min(a % b, b % a);
}
#define ONLINE_JUDGE
void solve() {
int n;
cin >> n;
vector <int> p(n);
for(int &i : p) {
cin >> i;
}
sort(p.begin(), p.end());
p.erase(unique(p.begin(), p.end()), p.end());
n = int(p.size());
using T = tuple <int, int, int>;
vector <T> edges;
for(int i = 0; i +1 < n; i++) {
for(int j = p[i]; j <= MAX; j += p[i]) {
int idx = lower_bound(p.begin() + i +1, p.end(), j) - p.begin();
if(idx == n)
break;
edges.emplace_back(i, idx, cost(p[i], p[idx]));
}
}
sort(edges.begin(), edges.end(), [&](const T &a, const T &b) -> bool {
return get <2> (a) < get <2> (b);
});
DSU dsu(n);
i64 ans = 0;
for(auto [u, v, c] : edges) {
//cerr << u << " " << v << " " << c << "\n";
if(dsu.unite(u, v)) {
ans += c;
}
}
cout << ans;
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
155 ms |
51800 KB |
Output is correct |
3 |
Correct |
3 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Runtime error |
785 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
28084 KB |
Output is correct |
2 |
Correct |
477 ms |
50996 KB |
Output is correct |
3 |
Correct |
223 ms |
51704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
4812 KB |
Output is correct |
2 |
Correct |
254 ms |
52076 KB |
Output is correct |
3 |
Correct |
147 ms |
28120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
294 ms |
52300 KB |
Output is correct |
2 |
Correct |
645 ms |
100428 KB |
Output is correct |
3 |
Correct |
199 ms |
26260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
7560 KB |
Output is correct |
2 |
Correct |
631 ms |
100512 KB |
Output is correct |
3 |
Correct |
198 ms |
26548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
177 ms |
27908 KB |
Output is correct |
2 |
Runtime error |
1524 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
199 ms |
26552 KB |
Output is correct |
2 |
Runtime error |
1383 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
3788 KB |
Output is correct |
2 |
Runtime error |
2009 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |