//author: Ahmet Alp Orakci
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAXN = 1E7 + 5;
ostream& operator<< (ostream& os, pair <int, int> &pii) {
return os << "{" << pii.first << " " << pii.second << "}";
}
struct DSU {
int n;
vector <int> par;
DSU(int n) : n(n) {
par.resize(n);
iota(par.begin(), par.end(), 0);
}
inline int get(int x) {
return par[x] = (x == par[x] ? x : get(par[x]));
}
inline bool same(int a, int b) {
return get(a) == get(b);
}
inline bool unite(int u, int v) {
if(same(u, v))
return false;
u = get(u);
v = get(v);
par[v] = u;
return true;
}
};
#define ONLINE_JUDGE
void solve() {
int n;
cin >> n;
vector <int> vec(n);
for(int &i : vec)
cin >> i;
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
n = vec.size();
vector <tuple <int, int, int>> edges;
for(int it = 0; it < n -1; it++) {
int i = vec[it];
for(int k = i; k < MAXN; k += i) {
int x = upper_bound(vec.begin() + it + 1, vec.end(), k -1) - vec.begin();
if(x == n)
break;
edges.emplace_back(vec[x] % i, it, x);
}
}
sort(edges.begin(), edges.end());
i64 ans = 0;
DSU dsu(n);
for(auto [c, u, v] : edges) {
if(dsu.unite(u, v)) {
ans += c;
}
}
cout << ans;
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "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 |
600 KB |
Output is correct |
2 |
Correct |
230 ms |
50896 KB |
Output is correct |
3 |
Correct |
3 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Runtime error |
572 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 |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
218 ms |
26432 KB |
Output is correct |
2 |
Correct |
832 ms |
51112 KB |
Output is correct |
3 |
Correct |
359 ms |
51276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
3536 KB |
Output is correct |
2 |
Correct |
375 ms |
51656 KB |
Output is correct |
3 |
Correct |
217 ms |
27308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
482 ms |
51356 KB |
Output is correct |
2 |
Correct |
1121 ms |
100368 KB |
Output is correct |
3 |
Correct |
322 ms |
26692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
7116 KB |
Output is correct |
2 |
Correct |
1066 ms |
101336 KB |
Output is correct |
3 |
Correct |
320 ms |
26040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
265 ms |
26688 KB |
Output is correct |
2 |
Runtime error |
1486 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
305 ms |
26036 KB |
Output is correct |
2 |
Runtime error |
1347 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
4812 KB |
Output is correct |
2 |
Runtime error |
1978 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |