//author: Ahmet Alp Orakci
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAXN = 2E7 + 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 = 2; i < MAXN; i++) {
if(ok[i]) {
for(int j = 2 * i; j < MAXN; j += i) {
if(sieve[j].first == 0)
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(vec[suf[i].second] - (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]);
}
}
sort(edges.begin(), edges.end());
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
156 ms |
391552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
445 ms |
472772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
155 ms |
393504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
658 ms |
481872 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
233 ms |
477564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
950 ms |
482156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
764 ms |
476804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
278 ms |
515528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
275 ms |
515568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
229 ms |
512588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |