//author: Ahmet Alp Orakci
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAXN = 1E7 + 5;
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]));
}
bool same(int a, int b) {
return get(a) == get(b);
}
bool unite(int u, int v) {
if(same(u, v))
return false;
u = get(u);
v = get(v);
par[v] = u;
return true;
}
};
pair <int, int> sieve[MAXN];
vector <bool> ok(MAXN);
pair <int, int> pref[MAXN];
pair <int, int> suf[MAXN];
int loc[MAXN];
int ptr;
#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();
if(vec.front() == 1)
return cout << 0, void();
for(int &i : vec)
ok[i] = true, loc[i] = ptr++;
DSU dsu(n);
for(int i = 1; i < MAXN; i++) {
if(ok[i]) {
for(int j = 2 * i; j < MAXN; j += i) {
sieve[j] = {i, loc[i]};
if(ok[j]) {
dsu.unite(loc[i], loc[j]);
}
}
}
}
pref[0] = {-1, -1};
for(int i = 1; i < MAXN; i++) {
if(!sieve[i].first)
pref[i] = pref[i -1];
else
pref[i] = {i, sieve[i].second};
}
suf[MAXN -1] = {-1, -1};
for(int i = MAXN -2; i >= 1; i--) {
if(!sieve[i].first)
suf[i] = suf[i +1];
else
suf[i] = {i, sieve[i].second};
}
vector <tuple <int, int, int>> edges;
i64 ans = 0;
for(int &i : vec) {
if(suf[i].first != -1) {
edges.emplace_back(suf[i].first - i, suf[i].second, loc[i]);
}
if(pref[i].first != -1) {
edges.emplace_back(i - pref[i].first, pref[i].second, loc[i]);
}
}
for(auto [c, u, v] : edges) {
//cerr << c << " " << u << " " << v << "\n";
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 |
Incorrect |
80 ms |
274856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
152 ms |
238416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
79 ms |
274824 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
208 ms |
248772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
87 ms |
243428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
294 ms |
248280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
228 ms |
242920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
107 ms |
281700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
107 ms |
281032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
86 ms |
276648 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |